forked from qiubaiying/qiubaiying.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfly.html
97 lines (97 loc) · 3.4 KB
/
fly.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
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
---
permalink: /fly
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
margin: 0;
padding: 0;
width: 100%;
background: url(sky.jpg);
}
#fly{
background: url(mes.png);
position: absolute;
top: 50%;
left: 50%;
}
</style>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<script>
var fly=document.createElement('div');
fly.id='fly';
fly.style.width='60px';
fly.style.height='60px';
document.body.appendChild(fly);
var skyx=0;
var skyt=0;
var c;
var j;
document.onmousemove=function(e){
var fly=document.getElementById('fly');
var contentx=fly.offsetLeft+fly.offsetWidth/2;
var contenty=fly.offsetTop+fly.offsetHeight/2;
var dx=e.clientX-contentx;
var dt=e.clientY-contenty;
c=Math.atan2(dx,dt);//取值
c=180*c/Math.PI;//转换角度
c=c*-1;//console.log(c);
function to(){//飞机运动
clearTimeout(j);
fly.style.transform='rotate('+c+'deg)';
switch(true){
case c<15&&c>-15://向下飞
skyt-=5;
document.body.style.backgroundPositionY=skyt+'px';
break;
case c>15&&c<75://向左下角飞
skyx+=3;
skyt-=3;
document.body.style.backgroundPositionX=skyx+'px';
document.body.style.backgroundPositionY=skyt+'px';
break;
case c>75&&c<105://向左飞
skyx+=5;
document.body.style.backgroundPositionX=skyx+'px';
break;
case c>105&&c<165://向左上角飞
skyx+=3;
skyt+=3;
document.body.style.backgroundPositionX=skyx+'px';
document.body.style.backgroundPositionY=skyt+'px';
break;
case c>165||c<-165://向上飞
skyt+=5;
document.body.style.backgroundPositionY=skyt+'px';
break;
case c<-15&&c>-75://向右下角飞
skyx-=3;
skyt-=3;
document.body.style.backgroundPositionX=skyx+'px';
document.body.style.backgroundPositionY=skyt+'px';
break;
case c<-75&&c>-105://向右飞
skyx-=5;
document.body.style.backgroundPositionX=skyx+'px';
break;
case c<-105&&c>-165://向右上角飞
skyx-=3;
skyt+=3;
document.body.style.backgroundPositionX=skyx+'px';
document.body.style.backgroundPositionY=skyt+'px';
break;
}
j=setTimeout(to,10);
}
to();
}
</script>
</body>
</html>