-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue.html
180 lines (170 loc) · 6.39 KB
/
vue.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>vue mouse</title>
<style>
html,
body {
text-align: center;
color: #000;
background-color: #353535;
}
* {
box-sizing: border-box;
}
#app {
width: 600px;
margin: auto;
margin-top: 100px;
}
.content {
float: left;
position: relative;
height: 150px;
width: 150px;
margin: 20px;
overflow: hidden;
background: #ccc;
}
.content .shade {
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
line-height: 100px;
color: #fff;
background: rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div id="app">
<mouse-hover style="background:aqua">
<div slot>mouse hover</div>
</mouse-hover>
<mouse-hover style="background:bisque">
<div slot>mouse hover</div>
</mouse-hover>
<mouse-hover style="background:cadetblue">
<div slot>mouse hover</div>
</mouse-hover>
<mouse-hover style="background:chocolate">
<div slot>mouse hover</div>
</mouse-hover>
<mouse-hover style="background:cornflowerblue">
<div slot>mouse hover</div>
</mouse-hover>
<mouse-hover style="background:darkkhaki">
<div slot>mouse hover</div>
</mouse-hover>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>
<script>
(function () {
const mouseHover = {
name: 'mouseHover',
template: `
<div class="content" @mouseenter="handleIn" @mouseleave="handleOut">
<div class="shade" ref="shade">
<slot></slot>
</div>
</div>
`,
data: () => {
return {}
},
methods: {
handleIn: function (e) {
const direction = this.direction(e);
this.animate(direction, 'in');
},
handleOut: function (e) {
const direction = this.direction(e);
this.animate(direction, 'out');
},
direction: function (e, type) {
const clientX = e.clientX;
const clientY = e.clientY;
const top = e.target.offsetTop;
const bottom = parseInt(top + e.target.offsetHeight);
const left = e.target.offsetLeft;
const right = parseInt(left + e.target.offsetWidth);
const absTop = Math.abs(clientY - top);
const absBottom = Math.abs(clientY - bottom);
const absLeft = Math.abs(clientX - left);
const absRight = Math.abs(clientX - right);
const min = Math.min(absTop, absBottom, absLeft, absRight);
let direction;
switch (min) {
case absTop:
direction = "top";
break;
case absBottom:
direction = "bottom";
break;
case absLeft:
direction = "left";
break;
case absRight:
direction = "right";
break;
};
return direction;
},
animate: function (direction, type) {
let top = 0,
left = 0;
if (type == 'in') {
this.$refs.shade.style.transition = 'none';
if (direction == 'top') {
top = '-100%';
left = 0;
} else if (direction == 'right') {
top = 0;
left = '100%';
} else if (direction == 'bottom') {
top = '100%';
left = 0;
} else if (direction == 'left') {
top = 0;
left = '-100%';
}
this.$refs.shade.style.top = top;
this.$refs.shade.style.left = left;
setTimeout(() => {
this.$refs.shade.style.transition = 'all .2s ease 0s';
this.$refs.shade.style.top = 0;
this.$refs.shade.style.left = 0;
}, 0)
} else if (type == 'out') {
if (direction == 'top') {
top = '-100%';
left = 0;
} else if (direction == 'right') {
top = 0;
left = '100%';
} else if (direction == 'bottom') {
top = '100%';
left = 0;
} else if (direction == 'left') {
top = 0;
left = '-100%';
}
this.$refs.shade.style.top = top;
this.$refs.shade.style.left = left;
}
}
}
}
Vue.component(mouseHover.name, mouseHover)
new Vue({
el: '#app'
})
})();
</script>
</body>
</html>