-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
191 lines (167 loc) · 4.04 KB
/
demo.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Js感应鼠标移入移出</title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<div class="wrap">
<div class="inner-img">
<div class="desc">
<p>2018</p>
新春快乐
狗年吉祥
</div>
</div>
<div class="inner-img">
<div class="desc">
<p>2018</p>
合家欢乐
</div>
</div>
<div class="inner-img">
<div class="desc">
<p>2018</p>
百福迎新
</div>
</div>
<div class="inner-img last-block">
<div class="desc">
<p>2018</p>
恭喜发财
</div>
</div>
<div class="inner-img">
<div class="desc">
<p>2018</p>
新春快乐
狗年吉祥
</div>
</div>
<div class="inner-img">
<div class="desc">
<p>2018</p>
合家欢乐
</div>
</div>
<div class="inner-img">
<div class="desc">
<p>2018</p>
百福迎新
</div>
</div>
<div class="inner-img last-block">
<div class="desc">
<p>2018</p>
恭喜发财
</div>
</div>
<div class="clear"></div>
</div>
</body>
<script src="./js/jquery-3.3.1.min.js"></script>
<script>
$(function(){
$(".wrap .inner-img").each(function(index){
var number = index%4;
$(this).css("background-image","url(./images/"+"0"+(number+1)+".png"+")");
});
});
//遮盖层的显示与隐藏
$(".inner-img").mouseenter(function(event){
//获取当前元素四边中心点坐标
// var left = $(this).offset().left;
// var top = $(this).offset().top;
var width = $(this)[0].clientWidth;
var height = $(this)[0].clientHeight;
var topPointer = {
x:Math.round(width/2),
y:0
},
rightPointer = {
x:Math.round(width),
y:Math.round(height/2)
},
bottomPointer = {
x:Math.round(width/2),
y:Math.round(height)
},
leftPointer = {
x:0,
y:Math.round(height/2)
};
var borderPointers = [topPointer,rightPointer,bottomPointer,leftPointer];
//获取鼠标进入当前元素的坐标
var currentPointer = {
x:event.offsetX,
y:event.offsetY
};
console.log("Direction: "+toDirection(width,height,currentPointer));
//重定位遮罩层
var pos = locateTopLayer(toDirection(width,height,currentPointer),width,height);
// $(selector).animate(styles,speed,easing,callback)
$(this).find(".desc").animate({
top:pos.top+"px",
left:pos.left+"px"
},1,"linear",function(){
$(this).animate({
top:0,left:0
},120,"linear");
});
});
$(".inner-img").mouseleave(function(){
var left = $(this)[0].clientWidth;
var top = $(this)[0].clientHeight;
var currentPointer = {
x:event.offsetX,
y:event.offsetY
};
var pos = locateTopLayer(toDirection(left,top,currentPointer),left,top);
console.log("leave: "+toDirection(left,top,currentPointer));
$(this).find(".desc").animate({
top:pos.top,
left:pos.left
},120,"linear");
});
//计算最小值,确定方向,上右下左分别时0,1,2,3
function toDirection(width,height,currentPointer){
var minIndex = 0,
halfWidth = Math.round(width/2),
halfHeight = Math.round(height/2);
var x = currentPointer.x,
y = currentPointer.y;
//将图片划分为四个区块,左上角区块
if(x <= halfWidth && y <= halfHeight){
return x > y ? 0 : 3;
}else if(x >= halfWidth && y <= halfHeight){
return (width - x) > y ? 0 : 1;
}else if(x >= halfWidth && y >= halfHeight){
return (width - x) > (height - y) ? 2 : 1;
}else if(x <= halfWidth && y >= halfHeight){
return x > (height - y) ? 2 : 3;
}else{
console.log("something error......");
// return 999;
}
}
//定位遮罩层函数
function locateTopLayer(directionIndex,width,height){
var position = {left:width,top:height};
if(directionIndex === 0){
position.left = 0;
position.top = -height;
}else if(directionIndex === 1){
position.top = 0;
}else if(directionIndex === 2){
position.left = 0;
}else if(directionIndex === 3){
position.top = 0;
position.left = -width;
}else{
console.log("获取方向索引有误");
}
return position;
}
</script>
</html>