1.滚动加载
function onScroll() { if (this.curPage > this.totalPage) return; var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; if (scrollTop + windowHeight > scrollHeight - 100) { // flagData防止触发多次 if (!this.flagData) return; this.flagData = false; this.getSingerList();//请求数据成功后 page+1, this.flagData = true; } }
2.获取特定对象的值,key忽略大小写
let options={ shareUserId:123,}// let shareUserId=options.shareUserid||options.shareUserId||options.shareUserID;for (let item in options){ if(item.toLowerCase()=='shareuserid'){ let shareUserId=options[item]; console.log(shareUserId);//123 }}
3. 获取地址中的参数值
function getUrlParam(name, url) { if (!url) url = location.href; name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(url); return results == null ? null : results[1];}
4.格式化时间
/** * 格式化时间 * @ date 时间参数,传入字符串 * @ fmt 时间显示格式,‘yyyy-MM-dd hh:mm:ss’ */function formatDate(time, fmt) { if(!time) return ''; var date = new Date(time); var o = { 'y+': date.getFullYear(), 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() }; for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) { var str = o[k] + ''; fmt = fmt.replace(RegExp.$1, (str.length === 1) ? ('0' + str) : str); } }; return fmt;}
5.版本号比较
function versionCompare(curV, reqV) { var arr1 = curV.split('.'); var arr2 = reqV.split('.'); //将两个版本号拆成数字 var maxL = Math.max(arr1.length, arr2.length); var pos = 0; //当前比较位 var diff = 0; //当前为位比较是否相等 //逐个比较如果当前位相等则继续比较下一位 while (pos < maxL) { diff = parseInt(arr1[pos]||0) - parseInt(arr2[pos]||0); if (diff != 0) break; pos++; } if (diff >= 0) { return true; } else { return false; } }