-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathResizeObserver.vue
More file actions
98 lines (87 loc) · 1.96 KB
/
Copy pathResizeObserver.vue
File metadata and controls
98 lines (87 loc) · 1.96 KB
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
<template>
<div class="resize-observer" tabindex="-1"></div>
</template>
<script>
import { getInternetExplorerVersion } from '../utils/compatibility'
let isIE
function initCompat () {
if (!initCompat.init) {
initCompat.init = true
isIE = getInternetExplorerVersion() !== -1
}
}
export default {
name: 'resize-observer',
methods: {
compareAndNotify () {
if (this._w !== this.$el.offsetWidth || this._h !== this.$el.offsetHeight) {
this._w = this.$el.offsetWidth
this._h = this.$el.offsetHeight
this.$emit('notify')
}
},
addResizeHandlers () {
this._resizeObject.contentDocument.defaultView.addEventListener('resize', this.compareAndNotify)
this.compareAndNotify()
},
removeResizeHandlers () {
if (this._resizeObject && this._resizeObject.onload) {
if (!isIE && this._resizeObject.contentDocument) {
this._resizeObject.contentDocument.defaultView.removeEventListener('resize', this.compareAndNotify)
}
delete this._resizeObject.onload
}
},
},
mounted () {
initCompat()
this.$nextTick(() => {
this._w = this.$el.offsetWidth
this._h = this.$el.offsetHeight
})
const object = document.createElement('object')
this._resizeObject = object
object.setAttribute('aria-hidden', 'true')
object.setAttribute('tabindex', -1)
object.onload = this.addResizeHandlers
object.type = 'text/html'
if (isIE) {
this.$el.appendChild(object)
}
object.data = 'about:blank'
if (!isIE) {
this.$el.appendChild(object)
}
},
beforeDestroy () {
this.removeResizeHandlers()
},
}
</script>
<style scoped>
.resize-observer {
position: absolute;
top: 0;
left: 0;
z-index: -1;
width: 100%;
height: 100%;
border: none;
background-color: transparent;
pointer-events: none;
display: block;
overflow: hidden;
opacity: 0;
}
.resize-observer >>> object {
display: block;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
overflow: hidden;
pointer-events: none;
z-index: -1;
}
</style>