总结的一些函数
1.一位数组切割成二维数组
hdandle: function(lists,pageSize){ var newLists = []; var temLists = []; if (lists.length > 0) { for (var i = 0, lth = lists.length; i < lth; i++) { lists[i].index = i; temLists.push(lists[i]); if ((i + 1) % pageSize === 0 || (i === lth - 1 && (i + 1) % pageSize !== 0)) { newLists.push(temLists); temLists = []; } } }; return newLists; }
2.判断链接是否为图片
function isImg(str){ var patt = /[.png|.jpg|.jpeg|.gif|.bmp|.webp|.ico|.svg]$/i; return patt.test( str.replace(/\s*/g,"") ); };
3.无线和pc的判断
function(){ var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"; var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"; var bIsMidp = sUserAgent.match(/midp/i) == "midp"; var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; var bIsAndroid = sUserAgent.match(/android/i) == "android"; var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){ //pc页面 }else { //无线页面 window.location.href="https://peixun.1688.com/cms/yysbfwx.html"; } }
4.根据时间戳返回刚刚,几分钟前,几小时前,几天前,几个月前(超过3个月显示时间)
timeago(dateTimeStamp) { // dateTimeStamp是一个时间毫秒,注意时间戳是秒的形式,在这个毫秒的基础上除以1000,就是十位数的时间戳。13位数的都是时间毫秒。 let minute = 1000 * 60; // 把分,时,天,周,半个月,一个月用毫秒表示 let hour = minute * 60; let day = hour * 24; let week = day * 7; // let halfamonth = day * 15; let month = day * 30; let now = new Date().getTime(); // 获取当前时间毫秒 console.log(now); let diffValue = now - dateTimeStamp;// 时间差 if (diffValue < 0) { return; } let result = ''; let minC = diffValue / minute; // 计算时间差的分,时,天,周,月 let hourC = diffValue / hour; let dayC = diffValue / day; let weekC = diffValue / week; let monthC = diffValue / month; if (monthC >= 1 && monthC <= 3) { result = ' ' + parseInt(monthC) + '月前'; } else if (weekC >= 1 && weekC <= 3) { result = ' ' + parseInt(weekC) + '周前'; } else if (dayC >= 1 && dayC <= 6) { result = ' ' + parseInt(dayC) + '天前'; } else if (hourC >= 1 && hourC <= 23) { result = ' ' + parseInt(hourC) + '小时前'; } else if (minC >= 1 && minC <= 59) { result = ' ' + parseInt(minC) + '分钟前'; } else if (diffValue >= 0 && diffValue <= minute) { result = '刚刚'; } else { let datetime = new Date(); datetime.setTime(dateTimeStamp); let Nyear = datetime.getFullYear(); let Nmonth = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1; let Ndate = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate(); // let Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours(); // let Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes(); // let Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds(); result = Nyear + '-' + Nmonth + '-' + Ndate; } return result; }