判断当前时间是否在某一区间之内:
isDuringDate(beginDateStr, endDateStr) {
var curDate = new Date(),
beginDate = new Date(beginDateStr),
endDate = new Date(endDateStr);
if (curDate >= beginDate && curDate <= endDate) {
return true;
}
return false;
}
//使用
this.isDuringDate('2021/9/22', '2021/10/31')
判断是否往上滚了一屏的高度
scrollBot() {
$(window).scroll(() => {
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
let phoneHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
if (scrollTop > phoneHeight) {
this.btnBot = true
} else {
this.btnBot = false
}
})
},
获取路径传的参数:
export const urlToJson = (url = window.location.href) => {
let obj = {},
index = url.indexOf('?'), // 看url有没有参数
params = url.substr(index + 1); // 截取url参数部分 id = 1 & type = 2
if(index != -1) { // 有参数时
let parr = params.split('&'); // 将参数分割成数组 ["id = 1 ", " type = 2"]
for(let i of parr) { // 遍历数组
let arr = i.split('='); // 1) i id = 1 arr = [id, 1] 2)i type = 2 arr = [type, 2]
obj[arr[0]] = arr[1]; // obj[arr[0]] = id, obj.id = 1 obj[arr[0]] = type, obj.type = 2
}
}
return obj;
}
将数组每几个一分:
group(array, subGroupLength) {
let index = 0;
let newArray = [];
while (index < array.length) {
newArray.push(array.slice(index, index += subGroupLength));
}
return newArray;
}
this.group(prizeList, 3)
效果:
arr=[1,2,3,4,5,6,7,8]
按三个一分:
arr=[[1,2,3],[4,5,6],[7,8]]
随机挑选数组中的某一项:
this.imgSloganInfo = AppData.imgSloganData[this.whichEvel][Math.floor(Math.random()*AppData.imgSloganData[this.whichEvel].length)];
这个例子的数组格式为:(数组里面套数组)
AppData.imgSloganData=[[{},{},{}],[{},{},{}],[{},{},{}]]
如果是普通的数组的话,代码如下:
this.imgSloganInfo = AppData.imgSloganData[Math.floor(Math.random()*AppData.imgSloganData.length)];
根据数组某一项进行排序:
export const getDataSort = function (data, name){
return data.sort(function(a,b){
return a[name] - b[name];
});
}
//使用
let data = common.getDataSort(res.rankingDay, 'ranking');
获取百分比:
export const getPercent = function(num, total) {
num = parseFloat(num);
total = parseFloat(total);
if (isNaN(num) || isNaN(total)) {
return "-";
}
total = total <= 0 ? 0 : (Math.round(num / total * 10000) / 100.00)
return total>100?100:total;
}