- 主页 > 生活百科 > >
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);}
推荐阅读
-
穿帮镜头|穿内衣就算了,输血管我也忍了,那个戴口罩的,真拿观众当傻子呢
-
中国新闻网|张建宗:控制疫情、重振经济是香港特区政府的首要任务
-
为啥知乎上的高质量答案普遍采用“卖萌”式的叙述风格
-
好吃|为什么辣椒炒肉不好吃?我总是忘了加它,难怪肉会柴,不够味!
-
-
护心医生张诗文|吃好,睡好,运动好,效果堪比降压药!
-
王者登哥:柱石夏侯惇,三国志11:曹操的从兄弟?文武双全夏侯渊
-
-
通畅|专治便秘的小方法,坚持30天,没想到肠道通畅,再也不发愁便秘
-
神秘性感 时尚首选 黑色经典 Angelababy抹胸连身裙穿出不一样的味道
-
酒店客房租金回落,澳门Q2旅游物价指数环比降逾10%
-
-
物理|精编2021高考物理超重点汇总,全章节42页,建议收藏打印!
-
-
皮皮宠PPPet|如何区分不同价位散装白酒的质量?
-
鲳鱼|厄瓜多尔冷冻鲳鱼内包装检出新冠病毒阳性 海关总署采取紧急措施
-
-
-
暴龙电竞|LOL新版本曝光!iG史诗级加强,看到卢锡安改动后粉丝心态崩了
-
3DM游戏网|《云顶之弈》10.16海盗枪手阵容搭配推荐