-
Notifications
You must be signed in to change notification settings - Fork 24
/
emlateMouseenter.html
108 lines (92 loc) · 2.43 KB
/
emlateMouseenter.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
<!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>Document</title>
<style>
.wrap {
width: 50%;
box-sizing: border-box;
float: left;
}
.wrap,
.list {
border: solid 1px green;
padding: 30px;
margin: 30px 0;
}
.list {
border: solid 1px red;
}
.list li {
border: solid 1px blue;
padding: 10px;
margin: 10px;
}
.count {
color: red;
}
</style>
</head>
<body>
<div class="wrap">
wrap, mouseenter
<ul class="mouseenter list">
count: <span class="count"></span>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
<div class="wrap">
wrap, emulate mouseenter,用mouseover模拟实现mouseenter
<ul class="emulate-mouseenter list">
count: <span class="count"></span>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
<script>
let $mouseenter = document.querySelector('.mouseenter')
let $emulateMouseenter = document.querySelector('.emulate-mouseenter')
let $enterCount = document.querySelector('.mouseenter .count')
let $emulateMouseenterCounter = document.querySelector('.emulate-mouseenter .count')
let addCount = function (ele, start) {
return function () {
ele.innerHTML = ++start
}
}
let docEle = document.documentElement
let contains = docEle.contains ? function (parent, node) {
return parent !== node && parent.contains(node)
} : function (parent, node) {
let result = parent !== node
if (!result) {
return result
}
if (result) {
while (node && (node = node.parentNode)) {
if (parent === node) {
return true
}
}
}
return false
}
let emulateMouseenterCallback = addCount($emulateMouseenterCounter, 0)
let emulateEnterOrLeave = function (callback) {
return function (e) {
let relatedTarget = e.relatedTarget
if (relatedTarget !== this && !contains(this, relatedTarget)) {
callback.apply(this, arguments)
}
}
}
$mouseenter.addEventListener('mouseenter', addCount($enterCount, 0), false)
$emulateMouseenter.addEventListener('mouseover', emulateEnterOrLeave(emulateMouseenterCallback), false)
</script>
</body>
</html>