判断页面刷新和关闭方法

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>刷新和关闭页面测试</title>
</head>
<body>
<script>
{
var pageLoadFlag = 0;
window.onbeforeunload = function (event) {
setTimeout(function () {
var txt = "你刷新了页面"
if (pageLoadFlag) {
txt = "你关闭了页面";
}
// 关闭页面在 LocalStorage 查看
localStorage.setItem('test', txt);
})
}
window.addEventListener('unload', function () {
pageLoadFlag++
})
}
</script>
</body>
</html>

10 进制和 2 进制互相转换

1
2
3
// 浮点数不行
var toTwoNumber=(35).toString(2)
var toTenNumber=parseInt(twoNumber,2)

获取页面window监听的事件名称

放在所有代码开头

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
/**
* 获取页面window监听的事件名称
* for(const eventname of listenerList){
* console.log(eventname)
* }
*/
((w)=>{
w.listenerList=new Set()
const _cache=w.addEventListener
const handler={
apply:(target,thisBinding,args)=>{
target()
listenerList.add(args[0])
// _cache.apply(w,args)
Reflect.apply(_cache,w,args)
}
}
w.addEventListener=new Proxy(()=>{
console.log('现在绑定了一个事件,通过 window.listenerList 可查看所有已绑定事件')
},handler)
})(window)
window.addEventListener('resize',function(){
console.log('窗口正在发生变化')
})
for(const eventname of listenerList){
console.log(eventname)
}

JS防抖与节流

防抖:上个请求未结束,下个请求就发出,可能会造成其它影响,因此需要防抖。
节流:防止一个请求在短时间内多次请求,如100ms请求了10次,浪费流量,因此需要节流。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 节流
let timer=null
function throttling(){
clearTimeout(timer)
timer=setTimeout(function(){
// 发起请求
},300)
}
// 防抖
let flag=null
function (){
if(flag){
return;
}
flag=true;
$.get('/api',function(){
flag=false;
},function(){
flag=false;
})
}

arguments数组转为参数

1
2
3
4
5
6
7
function a(a1,a2){
console.log(a1,a2)
}
function b(){
a.apply(null,arguments)
}
b(1,2,3,4,5,6) // 1 2

合并 JSON

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
28
29
30
31
32
var a={a:{b:{c:1}},b:2,c:3},b={a:{e:2},b:{}};
var util={
type:{
Object:'[object Object]',
Undefined:'[object Undefined]'
},
judge:function(d,t){return Object.prototype.toString.call(d)===this.type[t]},
isUndefined:function(d){return this.judge(d,'Undefined')},
isObject:function(d){return this.judge(d,'Object')},
isArray:function(d){return Array.isArray(d)},
mergeJson:function(oa,ob){
if(!util.isObject(oa)||!util.isObject(ob)){
return ob
}
for(var i in oa){
// ob[i] 不存在,或者为 undefined,则用 oa[i] 替换
if(util.isUndefined(ob[i])){
ob[i]=oa[i]
}
// 两个都为对象继续迭代
if(util.isObject(oa[i]) && util.isObject(ob[i])){
util.mergeJson(oa[i],ob[i])
}
if(util.isArray(oa[i])&&util.isArray(ob[i])){
ob[i].concat(oa[i])
}
}
return ob;
}
}
const merge=util.mergeJson;
const result=merge(a,b)

英文引号转中文引号

1
2
3
4
5
6
7
8
var strArr=str.split('"'),newStr='',flag=true;
while(titleArr.length){
newStr+=titleArr.shift();
if(titleArr.length){
newStr+=["“","”"][+(falg=!flag)]
}
}
console.log(newStr);

js减速/匀速运动

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

<style>
*{padding: 0;margin:0;}
ul,li{list-style: none;}
#outer{
height: 260px;
overflow: hidden;
position: relative;
margin: auto;
}
#inner{
width: 500%;
height:100%;
position: absolute;
left:0;
top:0;
}
#inner div{
width: 20%;
height: 100%;
float: left;
}
ul{
width: 320px;
height:50px;
margin:5px auto;
}
li{
width: 16%;
height: 100%;
margin: 0 2%;
background-color: brown;
font-size: 30px;
font-weight: bold;
line-height: 50px;
text-align: center;
color: white;
float: left;
cursor: pointer;
}
h2{
text-align: center;
padding:5px 0;
}
</style>
<div id="outer">
<div id="inner">
<div style="background-color: antiquewhite;">1</div>
<div style="background-color: aquamarine;">2</div>
<div style="background-color: cornsilk;">3</div>
<div style="background-color: tomato;">4</div>
<div style="background-color: yellowgreen;">5</div>
</div>
</div>
<h2>减速运动</h2>
<ul id="btn1"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
<h2>匀速运动</h2>
<ul id="btn2"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
<script>
/*
减速运动原理
0---1000
1000
(1000-100)/10
90
(1000-190)/10
81
(1000-190-81)/10
72.9
(1000-190-81-72.9)/10
65.61
(1000-190-81-72.9-65.61)/10
59.049
.
.
.
0
*/

var oLis1=document.getElementById("btn1").getElementsByTagName("li"); // 减速运动 按钮
var oLis2=document.getElementById("btn2").getElementsByTagName("li"); // 缓速运动 按钮
var oDiv=document.getElementById("outer"); // 最外围 DIV
var oInner=document.getElementById("inner"); // 滑动的 DIV

var divWid=1; // 自定义宽度 默认 100%;

var times=50; // 自定义速度

var open=true; // 缓速运动 开关

function divWidth(){
var oDivWid=Math.round(oDiv.parentNode.clientWidth*divWid);
oDiv.style.width=oDivWid+"px";
}
divWidth();
window.onresize=function(){
divWidth();
}

for(var i=0;i<oLis1.length;i++){
oLis1.item(i).nIndex=i;
oLis1.item(i).onmouseover=function(){
buffer(oInner,this.nIndex);
}
}
// 减速运动1
function buffer(ele,i){
var target=i*-oDiv.clientWidth;
window.clearTimeout(ele.timer);
var nSpeed = (target-ele.offsetLeft)/times;
var nSpeed = nSpeed > 0 ? Math.ceil(nSpeed) : Math.floor(nSpeed);
var nSpeed=(ele.offsetLeft+nSpeed)/oDiv.clientWidth*100;

ele.style.left=nSpeed+"%";

if(ele.offsetLeft!=target){
ele.timer=window.setTimeout(function(){buffer(ele,i);},0);
}
}

// 减速运动2
/*
function buffer(ele,i){
var target=-i*oDiv.clientWidth;
window.clearTimeout(ele.timer);

var speed = (target-ele.offsetLeft)/8;
console.log(target,ele.offsetLeft,target-ele.offsetLeft,speed);
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
console.log(target - ele.offsetLeft);
if (Math.abs(target - ele.offsetLeft) <= speed) { // 由于在停止的时候最后会出现小的缝隙,或者说没有完全的到达指定地点,所以要小于它的速度
ele.style.left = target + 'px'; // 在停止后填充缝隙。
}else {
ele.timer=window.setTimeout(function(){move(ele,i);},30);
}
ele.style.left=ele.offsetLeft + speed +"px";

}
*/


// 匀速运动
for(var i=0;i<oLis2.length;i++){
oLis2.item(i).nIndex=i;
oLis2.item(i).onmouseover=function(){
open=true
move(oInner,this.nIndex);
}
}
function move(ele,i) {

var target = -i * oDiv.clientWidth;
var nSpeed;
window.clearTimeout(ele.timer);
// -10 小于 -5
if(target < ele.offsetLeft){
nSpeed = ele.offsetLeft-oDiv.clientWidth/times;
if(nSpeed <= target){
open=false;
}
}else{
nSpeed = ele.offsetLeft+oDiv.clientWidth/times;
if(nSpeed >= target){
open=false;
}
}
nSpeed = (nSpeed/oDiv.clientWidth)*100;
ele.style.left=nSpeed+"%";
if(open==true){
ele.timer=window.setTimeout(function(){move(ele,i);},0);
}else{
ele.style.left=-i*100+"%";
}
}
</script>

js动画插件

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<div id="div1" style="{width: 100px;height: 100px;background: red;position: absolute;left:0;top:0;}"></div>
<script>
/*
var oDiv = document.getElementById("div1");
function move(ele,attr,target){
// left-->offsetLeft,top-->offsetTop,width-->offsetWidth,height-->offsetHeight
attr=attr.toLowerCase();
var strOffset='offset'+attr.substr(0,1).toUpperCase()+attr.substring(1).toLowerCase();

window.clearTimeout(ele.timer);
var nSpeed = (target-ele[strOffset])/40;
nSpeed=nSpeed>0?Math.ceil(nSpeed):Math.floor(nSpeed);
ele.style[attr]=ele[strOffset]+nSpeed+"px";

if(nSpeed){
ele.timer=window.setTimeout(function(){move(ele,attr,target);},0)
}
}
oDiv.onclick=function(){move(this,'top',650);}
*/ // ---> 下面是拆分后的
var oDiv = document.getElementById("div1");

function core(ele,attr,target){
attr=attr.toLowerCase();
var strOffset='offset'+attr.substr(0,1).toUpperCase()+attr.substring(1).toLowerCase();

var nSpeed = (target-ele[strOffset])/40;
nSpeed=nSpeed>0?Math.ceil(nSpeed):Math.floor(nSpeed);
ele.style[attr]=ele[strOffset]+nSpeed+"px";

return nSpeed;
}
function move(ele,obj){
window.clearTimeout(ele.timer);
var flag=0;
for(var attr in obj){
flag += core(ele,attr,obj[attr]);
}
if(flag){
ele.timer=window.setTimeout(function(){move(ele,obj);},0);
}
}
// height,width,top,left
var obj={height:300,left:100,width:800,top:200};
oDiv.onclick=function(){move(this,obj);}
</script>