-
Notifications
You must be signed in to change notification settings - Fork 0
/
TargetLock.vue
55 lines (55 loc) · 1.13 KB
/
TargetLock.vue
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
<script>
/* eslint-disable */
export default {
name: 'TargetLock',
props: ['targetElem'],
data() {
return {
position: {
x: null,
y: null,
}
};
},
watch: {
targetElem: {
handler: 'bindEvents',
immediate: true
}
},
created() {
console.log('CREATED');
},
destroyed() {
console.log('DESTROYED');
document.body.removeEventListener("mousemove", this.onMouseMove);
},
methods: {
onMouseMove(e) {
console.log('onMouseMove')
const {x, y} = this.targetElem.getBoundingClientRect();
this.position.x = x;
this.position.y = y;
},
bindEvents() {
console.log('bindEvents')
document.body.removeEventListener("mousemove", this.onMouseMove);
if (this.targetElem) {
document.body.addEventListener("mousemove", this.onMouseMove);
}
}
},
computed: {
center() {
const {x, y, width} = this.targetElem.getBoundingClientRect();
return x + (width / 2);
}
},
render() {
return this.$scopedSlots.default({
position: this.position,
center: this.center,
});
}
}
</script>