1.检测平台(设备)类型
let isWechat = /micromessenger/i.test(navigator.userAgent),
isWeibo = /weibo/i.test(navigator.userAgent),
isQQ = /qq\//i.test(navigator.userAgent),
isIOS = /(iphone|ipod|ipad|ios)/i.test(navigator.userAgent),
isAndroid = /android/i.test(navigator.userAgent);
2.常用的日期时间函数
function format_date(timeStamp) {
let date = new Date(timeStamp);
return date.getFullYear() + "年"
+ prefix_zero(date.getMonth() + 1) + "月"
+ prefix_zero(date.getDate()) + "日 "
+ prefix_zero(date.getHours()) + ":"
+ prefix_zero(date.getMinutes());
}
function prefix_zero(num) {
return num >= 10 ? num : "0" + num;
}
function format_time(timeStamp) {
let day = Math.floor(timeStamp / (24 * 3600 * 1000));
let leave1 = timeStamp % (24 * 3600 * 1000);
let hours = Math.floor(leave1 / (3600 * 1000));
let leave2 = leave1 % (3600 * 1000);
let minutes = Math.floor(leave2 / (60 * 1000));
let leave3 = leave2 % (60 * 1000);
let seconds = Math.floor(leave3 / 1000);
if (day) return day + "天" + hours + "小时" + minutes + "分";
if (hours) return hours + "小时" + minutes + "分" + seconds + "秒";
if (minutes) return minutes + "分" + seconds + "秒";
if (seconds) return seconds + "秒";
return "时间到!";
}
3.跨端事件处理
let isSupportTouch = ("ontouchstart" in document.documentElement) ? true : false;
let isSupportTouch = ("ontouchstart" in document.documentElement) ? true : false;
document.onkeydown = function(event) {
let target, code, tag;
if (!event) {
event = window.event;
target = event.srcElement;
code = event.keyCode;
if (code == 13) {
tag = target.tagName;
if (tag == "TEXTAREA") { return true; }
else { return false; }
}
}
else {
target = event.target;
code = event.keyCode;
if (code == 13) {
tag = target.tagName;
if (tag == "INPUT") { return false; }
else { return true; }
}
}
};
4.移动端适配方案
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
var fontSize = 20;
docEl.style.fontSize = fontSize + 'px';
var docStyles = getComputedStyle(docEl);
var realFontSize = parseFloat(docStyles.fontSize);
var scale = realFontSize / fontSize;
console.log("realFontSize: " + realFontSize + ", scale: " + scale);
fontSize = clientWidth / 667 * 20;
if(isIphoneX()) fontSize = 19;
fontSize = fontSize / scale;
docEl.style.fontSize = fontSize + 'px';
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
function isIphoneX(){
return /iphone/gi.test(navigator.userAgent) && (screen.height == 812 && screen.width == 375)
}
})(document, window);
5.xss预防方式
function entities(s) {
let e = {
'"': '"',
'&': '&',
'<': '<',
'>': '>'
}
return s.replace(/["<>&]/g, m => {
return e[m]
})
}
6.常用的js算法
function throttle(fun, delay) {
let last, deferTimer
return function (args) {
let that = this
let _args = arguments
let now = +new Date()
if (last && now < last + delay) {
clearTimeout(deferTimer)
deferTimer = setTimeout(function () {
last = now
fun.apply(that, _args)
}, delay)
}else {
last = now
fun.apply(that,_args)
}
}
}
function debounce(fun, delay) {
return function (args) {
let that = this
clearTimeout(fun.id)
fun.id = setTimeout(function () {
fun.call(that, args)
}, delay)
}
}
let Observer = (function(){
let t __messages = {};
return {
regist: function(type, fn) {
if(typeof __messages[type] === 'undefined') {
messages[type] = [fn];
}else {
__messages[type].push(fn);
}
},
fire: function(type, args) {
if(!__messages[type]){
return
}
let events = {
type: type,
args: args || {}
},
i = 0,
len = __messages[type].length;
for(;i<len;i++){
__messages[type][i].call(this, events);
}
},
remove: function(type, fn) {
if(__messages[type] instanceof Array){
let i = __messages[type].length -1;
for(;i>=0;i--){
__messages[type][i] === fn && __messages[type].splice(i, 1)
}
}
}
}
})();
function formatString(str, data) {
return str.replace(/\{\{(\w+)\}\}/g, function(match, key) {
return typeof data[key] === undefined ? '' : data[key]
})
}
function bubbleSort(arr) {
for (let i = arr.length - 1; i > 0; i--) {
for (let j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
return arr;
}
function swap(arr, indexA, indexB) {
[arr[indexA], arr[indexB]] = [arr[indexB], arr[indexA]];
}
function distinct(arr = testArr) {
return arr.filter((v, i, array) => array.indexOf(v) === i)
}