-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path方向渐变.html
135 lines (129 loc) · 3.42 KB
/
方向渐变.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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
ul {
margin: 100px auto;
width: 200px;
height: 200px;
}
li {
list-style: none;
width: 200px;
height: 200px;
position: relative;
overflow: hidden;
}
div {
background: orange;
width: 200px;
height: 200px;
}
span {
position: absolute;
top: 0;
left: -200px;
width: 100%;
height: 100%;
background: lightgreen;
}
</style>
<body>
<ul>
<li>
<div></div>
<span></span>
</li>
</ul>
</body>
<script>
var oUl = document.querySelector('ul');
var oLi = oUl.children[0];
var oSpan = oUl.querySelector('span');
oLi.onmouseenter = function (ev) {
var oEvent = ev || event;
var q = getMousePos(this, oEvent);
switch (q) {
case 0:
oSpan.style.left = '200px';
oSpan.style.top = '0';
break;
case 1:
oSpan.style.left = '0';
oSpan.style.top = '200px';
break;
case 2:
oSpan.style.left = '-200px';
oSpan.style.top = '0';
break;
case 3:
oSpan.style.left = '0';
oSpan.style.top = '-200px';
break;
}
move(oSpan, {left: 0, top: 0})
};
oLi.onmouseleave = function (ev) {
var oEvent = ev || event;
var q = getMousePos(this, oEvent);
switch (q) {
case 0:
move(oSpan, {left: 200, top: 0});
break;
case 1:
move(oSpan, {left: 0, top: 200});
break;
case 2:
move(oSpan, {left: -200, top: 0});
break;
case 3:
move(oSpan, {left: 0, top: -200});
break;
}
};
function getMousePos(obj, ev) {
var w = obj.offsetWidth;
var h = obj.offsetHeight;
var x = obj.offsetLeft + w / 2 - ev.pageX;
var y = obj.offsetTop + h / 2 - ev.pageY;
return Math.round((Math.atan2(y, x) * 180 / Math.PI + 180) / 90) % 4;
}
function move(obj, json) {
var start = {};
var dis = {};
for (var name in json) {
start[name] = parseFloat(getComputedStyle(obj, false)[name]);
switch (name) {
case 'left':
start[name] = obj.offsetLeft;
break;
case 'top':
start[name] = obj.offsetTop;
break;
}
dis[name] = json[name] - start[name];
}
var count = Math.round(300 / (1000 / 30));
var n = 0;
clearInterval(obj.timer);
obj.timer = setInterval(function () {
n++;
for (var name in json) {
var a = n / count;
var cur = start[name] + dis[name] * a;
obj.style[name] = cur + 'px';
}
if (n == count) {
clearInterval(obj.timer);
}
}, 1000 / 30)
}
</script>
</html>