-
Notifications
You must be signed in to change notification settings - Fork 0
/
时间.html
47 lines (42 loc) · 1016 Bytes
/
时间.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{width:50px;height:50px;background:red;position:absolute;}
</style>
</head>
<body>
<div></div>
<script>
let div=document.querySelector("div");
let date=new Date().getTime();
let bool=false;
let num=40;
div.addEventListener("click",clickHandler);
function clickHandler(e){
bool=!bool;
if(bool){
animation();
} else if(!bool){
cancelAnimationFrame(id)
}
}
function animation(){
id=requestAnimationFrame(animation);
/*if(new Date().getTime()-date >1000){
div.style.left=div.offsetLeft+2+"px";
console.log("1");
date=new Date().getTime();
}*/
num--;
if(!num){
div.style.left=div.offsetLeft+2+"px";
console.log(num);
num=1;
}
}
</script>
</body>
</html>