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;
}
}