- 主页 > 生活百科 > >
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);}
推荐阅读
-
直观视界|12在5000元以下,在5G的加持下,还能再创辉煌吗?,如果iPhone
-
[软件]联合国突然宣布,马云始料未及,腾讯也猝不及防!
-
-
充电头网 PD快充移动电源上市,打造国潮数码精品,故宫元素30W
-
#特朗普#新冠确诊150万,美国再遇突发重大紧急事件,洛杉矶爆炸腾蘑菇云
-
回忆传奇那些青春@热血传奇176复古金币武器喝幸运7的小技巧
-
【腾讯】“有图有真相”未必靠谱?看腾讯如何告赢这起侵权案
-
-
制造业|8月财新制造业PMI创新高,经济复苏向好动力强劲
-
张庭|张庭夫妇相关传销案听证会排期中,所涉金额或逾100亿元
-
视线|画面触目惊心!女子惨遭货车碾压身亡,事发时她正在……
-
好喜欢这个“爹”!钟汉良藏海传造型,留了胡子,权谋感满满!
-
-
3DMGAME|纪念碑谷团队新作《阿尔芭:野生动物冒险》12月12日登陆iOS/PC
-
-
2023美妆口碑榜深度解读 从裸妆美拉德卷出的彩妆潮流
-
讯美传媒柏康|十款便宜又好用的面膜排行榜,面膜哪个牌子好用不贵
-
相亲,余磊,约会|相亲宴中,女人“拖家带口”去蹭饭,男人逃单:走为上计绝不扶贫
-
-