-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
49 lines (48 loc) · 1.09 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Custom Directives</title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<h2>Click Outside</h2>
<div v-click-outside="clickedOutside" class="box">
Here is some text
</div>
</div>
<script type="text/javascript">
new Vue({
el: "#app",
data: {},
directives: {
clickOutside: {
bind (el, binding) {
el.__ClickOutsideHandler__ = event => {
if (!(el === event.target || el.contains(event.target))) {
binding.value(event)
}
}
document.body.addEventListener('click', el.__ClickOutsideHandler__)
},
unbind (el) {
document.body.removeEventListener('click', el.__ClickOutsideHandler__)
}
}
},
methods: {
clickedOutside () {
alert('you clicked outside 👏')
}
}
})
</script>
<style type="text/css">
.box {
border: 2px solid purple;
width: 200px;
height: 100px;
}
</style>
</body>
</html>