-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhover.js
80 lines (77 loc) · 2.43 KB
/
hover.js
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
function toEdgeDist(x,y,x2,y2){
const xDiff = x-x2;
const yDiff = y-y2;
return (xDiff**2 + yDiff**2);
//算出到四方形某条边中点的位置
}
//calculate the closest middle point
const closestDist = function(x,y,w,h){
const toTop = toEdgeDist(x,y,w/2,0);
const toBottom = toEdgeDist(x,y,w/2,h);
const toLeft = toEdgeDist(x,y,0,h/2);
const toRight = toEdgeDist(x,y,w,h/2);
const minDist = Math.min(toTop,toBottom,toLeft,toRight);
switch(minDist){
case toTop:
return 'top';
case toBottom:
return 'bottom';
case toLeft:
return 'left';
case toRight:
return 'right';
}
}
$(document).ready(function(){
const num = $(".boxes").length;
for( let i = 0; i<num; i++){
$('.boxes').eq(i).mouseenter(function(e){
const x = e.pageX - this.offsetLeft;
const y = e.pageY - this.offsetTop;
const w = this.clientWidth;
const h = this.clientHeight;
const enterResult = closestDist(x,y,w,h);
console.log(x,y,w,h);
switch(enterResult){
case 'top':
$('.overlay').eq(i).css({'top':'-100%','left':'0%'});
$('.overlay').eq(i).animate({top:'0%'},300);
break;
case 'bottom':
$('.overlay').eq(i).css({'top':'100%','left':'0%'});
$('.overlay').eq(i).animate({top:'0%'},300);
break;
case 'left':
$('.overlay').eq(i).css({'left':'-100%','top':'0%'});
$('.overlay').eq(i).animate({left:'0%'},300);
break;
case 'right':
$('.overlay').eq(i).css({'left':'100%','top':'0%'});
$(".overlay").eq(i).animate({left:'0%'},300);
break;
}
});
$('.boxes').eq(i).mouseleave(function(e){
$('.overlay').eq(i).css({'top':'0%','left':'0%'});
const x = e.pageX - this.offsetLeft;
const y = e.pageY - this.offsetTop;
const w = this.clientWidth;
const h = this.clientHeight;
const exitResult = closestDist(x,y,w,h);
switch (exitResult){
case 'top':
$('.overlay').eq(i).animate({'top':'-100%'},300);
break;
case 'left':
$('.overlay').eq(i).animate({'left':'-100%'},300);
break;
case 'right':
$('.overlay').eq(i).animate({'left':'100%'},300);
break;
case 'bottom':
$('.overlay').eq(i).animate({'top':'100%'},300);
break;
}
})
}
})