Skip to content

Commit 7109b6d

Browse files
DEaling with element with parent not yet rendered
1 parent a4c910a commit 7109b6d

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

example/components/Hello.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<template>
22
<div class="hello">
3-
<div class="container">
3+
<label>Show examples: <input type="checkbox" v-model="showExample" /></label>
4+
5+
<div class="container" v-show="showExample">
46
<div>
57
<div>
68
<b>Resize count : {{resizeCount}} </b>
@@ -29,7 +31,7 @@
2931
</div>
3032
</div>
3133

32-
<div class="container">
34+
<div class="container" v-show="showExample">
3335
<div>
3436
<div>
3537
<b>Resize count : {{resizeInitialCount}} </b>
@@ -86,7 +88,8 @@ export default {
8688
resizeIfCount: 0,
8789
resizeInitialCount: 0,
8890
show: false,
89-
display: false
91+
display: false,
92+
showExample: false
9093
};
9194
},
9295
methods: {

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"dependencies": {
2222
"css-element-queries": "^1.0.2",
23+
"intersection-observer": "^0.5.0",
2324
"lodash.debounce": "^4.0.8"
2425
},
2526
"devDependencies": {

src/Vueresize.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ResizeSensor from 'resizeSensor'
22
import lodashDebounce from 'lodash.debounce'
33
require('./findPolyfill');
4+
require('intersection-observer');
45

56
const { debounce = lodashDebounce } = lodashDebounce;
67
const defaultDelay = 150;
@@ -15,6 +16,24 @@ function getOptions(modifiers) {
1516
return { delay, initial }
1617
}
1718

19+
function listenToVisible(element, callback) {
20+
const options = {
21+
root: document.documentElement
22+
}
23+
24+
const observer = new IntersectionObserver((entries, observer) => {
25+
entries.forEach(entry => {
26+
if (entry.isIntersecting) {
27+
callback();
28+
observer.disconnect();
29+
}
30+
});
31+
}, options);
32+
33+
observer.observe(element);
34+
return observer;
35+
}
36+
1837
function createResizeSensor(el, { value, arg, modifiers }) {
1938
let callBack = () => value(el);
2039
const options = getOptions(modifiers);
@@ -41,9 +60,16 @@ export default {
4160
console.warn('v-resize should received a function as value');
4261
return;
4362
}
44-
createResizeSensor(el, { value, arg, modifiers });
63+
if (el.offsetParent) {
64+
createResizeSensor(el, { value, arg, modifiers });
65+
return;
66+
}
67+
el.__visibility__listener__ = listenToVisible(el, () => createResizeSensor(el, { value, arg, modifiers }))
4568
},
4669
unbind(el) {
70+
if (el.__visibility__listener__) {
71+
el.__visibility__listener__.disconnect();
72+
}
4773
if (!el.resizeSensor) {
4874
return;
4975
}

0 commit comments

Comments
 (0)