- 主页 > 生活百科 > >
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);}
推荐阅读
-
龙丹妮手中最大一张牌,美国豪宅堪比博物馆,国内老家还有瞭望塔
-
ZAKER|未来2年内开建!,34.3GW平价风电项目
-
-
晚安曲推荐|著名导演邀请吴彦祖,欲拍《赌王传》,遭何超仪大骂有违道德,原创
-
光明网|重温抗日战争伟大历史《海报里的英雄》开播在即
-
-
-
-
扎哈维|重磅!富力对一线队名单进行了重大调整,金靴射手被正式撤出名单
-
陈晓|古装帅哥热度榜:肖战第一、胡歌第五、王一博第八,王鹤棣落榜
-
如果一个人从小到大只获得正向反馈(想要的都得到,想做的都成功),他是否能获得「反思」的能力?
-
鹏飞奇闻变得年轻漂亮,关键秘诀就藏在我们的细胞里,想要延年益寿
-
-
冰封千里266|要买电视的朋友了,可以仔细看我的文章,双11要来了
-
-
品恒星语|您的“贵人”已到家中,别让他们失望!,属羊人:快行动起来
-
电影|漫威和DC联动 滚导想拍小丑女和格鲁特电影?本人回应
-
-
「龙江六道尖」但拿手机的方式代表了一个人性格,看看你属于哪方面,手机普遍了
-
『央视财经』跌幅1.06%;央视50指数报6888.00点,跌幅0.82%,收盘:沪指报2808.53点