-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
65 lines (57 loc) · 2 KB
/
main.js
File metadata and controls
65 lines (57 loc) · 2 KB
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
window.onload=function(){
let index=0; //默认显示第一张
let allA=document.getElementsByTagName("a");
allA[index].style.backgroundColor="black";
let imgList=document.getElementsByClassName("imgList")[0];
let autoTimer;
function autoChange(){
autoTimer=setInterval(function(){ //自动切换
index++;
move(imgList,-400*index,15,"left",function(){
set();
});
},2000);
};
for (let i=0;i<allA.length;i++) //手动切换
{
allA[i].onclick=function(){
clearInterval(autoTimer); //用户点击需要关闭自动定时器
index=i;
set();
move(imgList,-400*index,15,"left",autoChange); //点击效果结束需要打开自动定时器
}
}
autoChange();
function set(){
if (index>=5){
index=0;
imgList.style.left="0px";
}
for (let i=0;i<allA.length;i++){
allA[i].style.backgroundColor="";
}
allA[index].style.backgroundColor="black";
}
function move(obj,target,speed,attr,callback){
clearInterval(obj.timer); //防抖
let currentPlace=window.getComputedStyle?parseInt(getComputedStyle(obj,null)[attr]):parseInt(obj.currentStyle[attr]);
if (currentPlace>target){
speed=-speed;
}
obj.timer=setInterval(function(){
let lastValue=window.getComputedStyle?parseInt(getComputedStyle(obj,null)[attr]):parseInt(obj.currentStyle[attr]);
if (lastValue==target){
return;
}
let newValue=lastValue + speed;
if (speed<0&&newValue<target||speed>0&&newValue>target){
newValue=target;
}
obj.style[attr]=newValue+"px";
if (newValue==target){
clearInterval(obj.timer); //自结束
callback && callback();
}
},10);
}
}