Js常用 方法 封装
// 监听滚动,用于列表页向下加载--------------------------------- function loadmore(callback) { $(window).scroll(function () { var scrollTop = $(this).scrollTop(); //滚动条距离顶部的高度 var scrollHeight = $(document).height(); //当前页面的总高度 var clientHeight = $(this).height(); //当前可视的页面高度 //距离顶部+当前高度 >=文档总高度 即代表滑动到底部 if (scrollTop + clientHeight >= scrollHeight - 200) { callback && callback() } }); } // toast弱提示 提示框-------------------------------------- function toast(config) { var mask = document.getElementById("toast_mask"); var func = config.func; if (!mask) { mask = document.createElement("div"); mask.id = "toast_mask"; mask.className = "toast_mask f26"; mask.innerHTML = '<div class="toast_box" id="toast_box">' + '<div class="tips_content" id="toast_info">' + config.msg + '</div>' + '</div>'; document.body.appendChild(mask); } else { mask.style.display = "flex"; } //延时消失 setTimeout(() => { mask.style.display = "none"; func && func(); }, config.duration || 3000) //点击时消失 mask.onclick = () => { mask.style.display = "none" } document.getElementById("toast_info").innerText = config.msg; }
//过滤数组重复项--------------------------------------------------------------- let getNo_list = () => { var arr = [1, 2, 3, 1, 3, 4, 5, 5]; var resultArr = []; for (i = 0; i < arr.length; i++) { for (j = 0; j < resultArr.length; j++) { if (resultArr[j] == arr[i]) { break; } } if (j == resultArr.length) { resultArr[resultArr.length] = arr[i]; } } console.log(resultArr); //1,2,3,4,5 }
//数组排序 从小到大--------------------------------------------------------- let list_isBig = (arr) => { for (var j = 0; j < arr.length - 1; j++) { //两两比较,如果前一个比后一个大,则交换位置。 for (var i = 0; i < arr.length - 1 - j; i++) { if (arr[i] > arr[i + 1]) { var temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } } return arr }
//获取json长度---------------------------------------------------------- let getJsonObjLength = (jsonObj) => { var Length = 0; for (var item in jsonObj) { Length++; } return Length; }
//传入一个开始时间('2019','1')返回一个时间戳区间 let computeTime = (year, month) => { return [ // new Date(year, month - 1, 1).getTime(), // new Date(year, month, 0).getTime() + (24 * 60 * 60 * 1000 - 1000) 注释的和非注释都可用 new Date(year, month - 1, 1).getTime() / 1000, new Date(year, month, 1).getTime() / 1000 - 1 ] }
//传入时间戳,第二个参数是格式,也可不传 let parseTime = (time, pattern) => { if (!time) { return null } const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time) } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } if (result.length > 0 && value < 10) { value = '0' + value } return value || 0 }) return time_str }
// 复制 function copy(item) { if (isIosorAn() == "ios") { is_iosCopy(item) } else { var text = document.createElement("input"); text.id = "sharecopy"; text.value = item; text.style.position = "fixed"; text.style.top = "-10000px"; text.readOnly = false; document.body.appendChild(text); text.select(); var copys = document.execCommand("copy"); text.blur(); text.remove(); if (copys) { toast({ msg: "复制成功" }) } else { toast({ msg: "复制失败" }) } } } function is_iosCopy(item) { var el = document.createElement('input'); el.value = item el.style.opacity = '0'; document.body.prepend(el); el.type = "text" var editable = el.contentEditable; var readOnly = el.readOnly; el.contentEditable = true; el.readOnly = false; var range = document.createRange(); range.selectNodeContents(el); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); el.setSelectionRange(0, 999999); el.contentEditable = editable; el.readOnly = readOnly; var ret = document.execCommand('copy'); el.blur(); el.remove() if (ret) { toast({ msg: "复制成功" }) } else { toast({ msg: "复制失败" }) } } function isIosorAn() { var equipmentType = ""; var agent = navigator.userAgent.toLowerCase(); var android = agent.indexOf("android"); var iphone = agent.indexOf("iphone"); var ipad = agent.indexOf("ipad"); if (android != -1) { equipmentType = "android"; } if (iphone != -1 || ipad != -1) { equipmentType = "ios"; } return equipmentType; }
//监听ios软键盘收起 document.body.addEventListener('focusout', () => { //软键盘收起的事件处理 setTimeout(() => { const scrollHeight = document.documentElement.scrollTop || document.body.scrollTop || 0; window.scrollTo(0, Math.max(scrollHeight - 1, 0)); }, 100); });