- 主页 > 生活百科 > >
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);}
推荐阅读
-
帅哥|中国最早通过“国考”录用的公务员是这打扮,帅哥秒变大叔
-
-
[非洲]泔水喂猪危害大,还能引起非洲猪瘟?网友疑惑:为啥以前
-
第一财经|央行数字货币完成技术储备 何时落地 有何影响? | 财经夜行线
-
明星八卦谢霆锋对她一见钟情,陈冠希为追她直接戒烟,网友:幸亏都没答应
-
星座人缘知识汇|难忘彼此,爱情重回头的星座!,旧爱刻入骨髓
-
-
「吃了吗你呢」5道色香味俱佳的美味家常菜,回家吃饭,每天都有新惊喜!
-
「驱动之家」小米小背包销量破500万:连续3年箱包品牌第一,24.9元爆品
-
-
-
月月玩游戏|推荐优先兑换引导石,最多可获2400个,DNF:沙滩排球大作战攻略
-
李世民女儿高阳公主为什么会爱上一个和尚-?大唐的高阳公主嫁给了谁
-
-
现在的时机, 是不是适合卖老家房子然后上海首付7层?
-
-
Wi-Fi|雷军安利Redmi Note 11!WiFi 6普及到Note系列
-
娱乐龙卷风|直言弄坏了可以修复,网友:不火是有原因的!,袁姗姗手捏文物不听劝
-
-
烹饪|大热天的,吃鱼吃肉不如喝这汤,比羊肉滋补,比排骨美