- 主页 > 生活百科 > >
36个工作中常用的JavaScript函数片段( 二 )
- 摘自 https://juejin.im/post/5e58f398f265da574a1eb569
返回当前网页地址
function currentURL() {return window.location.href;}获取滚动条位置
function getScrollPosition(el = window) {return {x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop,};}获取 url 中的参数
function getURLParameters(url) {return url.match(/([^?=&]+)(=([^&]*))/g).reduce((a, v) => ((a[v.slice(0, v.indexOf("="))] = v.slice(v.indexOf("=") + 1)), a),{});}页面跳转 , 是否记录在 history 中
function redirect(url, asLink = true) {asLink ? (window.location.href = https://www.isolves.com/it/cxkf/yy/js/2020-05-14/url) : window.location.replace(url);}滚动条回到顶部动画
function scrollToTop() {const scrollTop =document.documentElement.scrollTop || document.body.scrollTop;if (scrollTop > 0) {window.requestAnimationFrame(scrollToTop);window.scrollTo(0, c - c / 8);} else {window.cancelAnimationFrame(scrollToTop);}}复制文本
function copy(str) {const el = document.createElement("textarea");el.value = https://www.isolves.com/it/cxkf/yy/js/2020-05-14/str;el.setAttribute("readonly", "");el.style.position = "absolute";el.style.left = "-9999px";el.style.top = "-9999px";document.body.AppendChild(el);const selected =document.getSelection().rangeCount > 0? document.getSelection().getRangeAt(0): false;el.select();document.execCommand("copy");document.body.removeChild(el);if (selected) {document.getSelection().removeAllRanges();document.getSelection().addRange(selected);}}检测设备类型
function detectDeviceType() {return /Android|webOS|iphone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)? "Mobile": "Desktop";}Cookie增
function setCookie(key, value, expiredays) {var exdate = new Date();exdate.setDate(exdate.getDate() + expiredays);document.cookie =key +"=" +escape(value) +(expiredays == null ? "" : ";expires=" + exdate.toGMTString());}删
function delCookie(name) {var exp = new Date();exp.setTime(exp.getTime() - 1);var cval = getCookie(name);if (cval != null) {document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();}}查
function getCookie(name) {var arr,reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");if ((arr = document.cookie.match(reg))) {return arr[2];} else {return null;}}日期 Date时间戳转换为时间
- 默认为当前时间转换结果
- isMs 为时间戳是否为毫秒
function timestampToTime(timestamp = Date.parse(new Date()), isMs = true) {const date = new Date(timestamp * (isMs ? 1 : 1000));return `${date.getFullYear()}-${date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;} 文档对象 DOM固定滚动条
/** * 功能描述:一些业务场景 , 如弹框出现时 , 需要禁止页面滚动 , 这是兼容安卓和 IOS 禁止页面滚动的解决方案 */let scrollTop = 0;function preventScroll() {// 存储当前滚动位置scrollTop = window.scrollY;// 将可滚动区域固定定位 , 可滚动区域高度为 0 后就不能滚动了document.body.style["overflow-y"] = "hidden";document.body.style.position = "fixed";document.body.style.width = "100%";document.body.style.top = -scrollTop + "px";// document.body.style['overscroll-behavior'] = 'none'}function recoverScroll() {document.body.style["overflow-y"] = "auto";document.body.style.position = "static";// document.querySelector('body').style['overscroll-behavior'] = 'none'window.scrollTo(0, scrollTop);}判断当前位置是否为页面底部
function bottomVisible() {return (document.documentElement.clientHeight + window.scrollY >=(document.documentElement.scrollHeight ||document.documentElement.clientHeight));}判断元素是否在可视范围内
- partiallyVisible 为是否为完全可见
function elementIsVisibleInViewport(el, partiallyVisible = false) {const { top, left, bottom, right } = el.getBoundingClientRect();return partiallyVisible? ((top > 0 && top < innerHeight) ||(bottom > 0 && bottom < innerHeight)) &&((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)): top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;}获取元素 css 样式
function getStyle(el, ruleName) {return getComputedStyle(el, null).getPropertyValue(ruleName);}
推荐阅读
-
-
-
许昕|绝对大翻盘!许昕孙颖莎4-3逆转超级黑马,进4强仍被央视解说批评
-
科技圈|卖19999元红米可能还不挣钱,亏本打广告!,小米合伙人爆料
-
实锤社|女明星高秋梓被打崩溃自闭!绝望大喊:太难玩了,QQ明星赛出事故
-
##张文宏谈新冠疫苗: 前期进展快 3期临床研究失去机会
-
误入武汉大连小伙客串电视剧究竟怎么回事?误入武汉大连小伙客串电视剧背后的真相
-
手机大魔王▲网友表示3499元不能再多了,魅族17携90HZ、骁龙865开售
-
-
-
小花聊美食■美食精选:香菇韭菜、烧排骨、角瓜鸡蛋、开口笑
-
-
DYG|DYG回归三线优势阵容,久诚元歌单杀暴风锐,giao成了五号位
-
烤箱烤吐司温度时间多少合适 烤箱烤吐司温度时间多少
-
黑产曝光!你扔掉的快递面单,被明码标价售卖!犯罪团伙“卧底”快递公司,有人一天“赚”超300元...
-
明朝被李自成打败的还是清朝打败的 李自成与清军大战为何大败
-
-
郭碧婷爸爸美食招待向佐|给半子!郭碧婷爸爸美食招待向佐心情大好 翁婿感情十分融洽
-
-
第一财经|11月第一周“风起云涌” 投资者如何接招?