命名空间 作用:避免全局污染

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(function () {
var _NS = function () {}

_NS.prototype.html = function (obj,value) {
var isArray=this.isArrayLike(obj), i=0;

if (typeof value == 'string') {
if (!isArray) {
obj.innerHTML = value;
} else {
var length = obj.length;
while (i < length) {
obj[i].innerHTML = value;
i += 1;
}
}
} else {
if (!isArray) {
return obj.innerHTML;
} else {
return obj[0].innerHTML;
}
}
}

window.NS = new _NS();
})();

js常用库函数的npm

1.在当前项目下安装

1
npm install --save-dev outils

2.按需引入

1
2
const getOS = require('outils/getOS')
const OS = getOS()

获取一个字符串的字节长度

1
2
3
4
5
6
7
8
function GetBytes(str) {
var len = str.length;
for (var i = 0; i < len; i++) {
//console.log(str[i],str.charCodeAt(i));
if (str.charCodeAt(i) > 255) len++;
}
return len;
}

求一个数组的最大值

Math.max.apply(null, arr)

监听屏幕的变化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var mql = window.matchMedia('(orientation: portrait)');
console.log(mql);

function handleOrientationChange(mql) {
if (mql.matches) {
console.log('portrait'); // 竖屏
} else {
console.log('landscape'); // 横屏
}
}
// 输出当前屏幕模式
handleOrientationChange(mql);
// 监听屏幕模式变化
mql.addListener(handleOrientationChange);

安卓手机在input输入框获得焦点的时候,整个页面上移

1
2
3
4
5
6
7
8
9
10
// window.onload 或者$(function() {})中添加
var u = navigator.userAgent;
if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机
var input = document.querySelector('input')
input.addEventListener('focus', function () {
setInterval(function () {
input.scrollIntoView(true); // 核心
}, 100)
})
}

使用正则实现一个千位符(从右向左,每隔三位插入一个逗号)

1
2
3
 vartoThousands = function(number) {
return (number + '').replace(/(\d)(?=(\d{3})+$)/g, '$1,');
}

移动端滚动穿透的问题

1
2
3
4
var modal = document.querySelector('.mask'); // 弹窗dom对象
modal.addEventListener('touchmove', function(e) {
e.preventDefault();
}, false);

这种方式的缺点是:当前元素也没法滚动

禁止双击选中

1
$('body').bind("selectstart", function() {return false;});