Skip to content

Commit 72adc49

Browse files
committed
Fixed #282, #285 - fix issue with detecting invisible elements.
1 parent 8f127c7 commit 72adc49

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

src/ResizeSensor.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
return globalWindow.setTimeout(fn, 20);
3737
};
3838

39+
var cancelAnimationFrame = globalWindow.cancelAnimationFrame ||
40+
globalWindow.mozCancelAnimationFrame ||
41+
globalWindow.webkitCancelAnimationFrame ||
42+
function (timer) {
43+
globalWindow.clearTimeout(timer);
44+
};
45+
3946
/**
4047
* Iterate over each of the provided element(s).
4148
*
@@ -102,8 +109,9 @@
102109
* @constructor
103110
*/
104111
var ResizeSensor = function(element, callback) {
105-
var lastAnimationFrame = 0;
106-
112+
//Is used when checking in reset() only for invisible elements
113+
var lastAnimationFrameForInvisibleCheck = 0;
114+
107115
/**
108116
*
109117
* @constructor
@@ -201,12 +209,15 @@
201209
element.style.position = 'relative';
202210
}
203211

204-
var dirty, rafId;
212+
var dirty = false;
213+
214+
//last request animation frame id used in onscroll event
215+
var rafId = 0;
205216
var size = getElementSize(element);
206217
var lastWidth = 0;
207218
var lastHeight = 0;
208219
var initialHiddenCheck = true;
209-
lastAnimationFrame = 0;
220+
lastAnimationFrameForInvisibleCheck = 0;
210221

211222
var resetExpandShrink = function () {
212223
var width = element.offsetWidth;
@@ -228,10 +239,9 @@
228239
var invisible = element.offsetWidth === 0 && element.offsetHeight === 0;
229240
if (invisible) {
230241
// Check in next frame
231-
if (!lastAnimationFrame){
232-
lastAnimationFrame = requestAnimationFrame(function(){
233-
lastAnimationFrame = 0;
234-
242+
if (!lastAnimationFrameForInvisibleCheck){
243+
lastAnimationFrameForInvisibleCheck = requestAnimationFrame(function(){
244+
lastAnimationFrameForInvisibleCheck = 0;
235245
reset();
236246
});
237247
}
@@ -282,8 +292,11 @@
282292
addEvent(expand, 'scroll', onScroll);
283293
addEvent(shrink, 'scroll', onScroll);
284294

285-
// Fix for custom Elements
286-
lastAnimationFrame = requestAnimationFrame(reset);
295+
// Fix for custom Elements and invisible elements
296+
lastAnimationFrameForInvisibleCheck = requestAnimationFrame(function(){
297+
lastAnimationFrameForInvisibleCheck = 0;
298+
reset();
299+
});
287300
}
288301

289302
forEachElement(element, function(elem){
@@ -292,9 +305,9 @@
292305

293306
this.detach = function(ev) {
294307
// clean up the unfinished animation frame to prevent a potential endless requestAnimationFrame of reset
295-
if (!lastAnimationFrame) {
296-
window.cancelAnimationFrame(lastAnimationFrame);
297-
lastAnimationFrame = 0;
308+
if (!lastAnimationFrameForInvisibleCheck) {
309+
cancelAnimationFrame(lastAnimationFrameForInvisibleCheck);
310+
lastAnimationFrameForInvisibleCheck = 0;
298311
}
299312
ResizeSensor.detach(element, ev);
300313
};

tests/demo.html

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@
9797
.example-2[min-width~="400px"] .example-2-box {
9898
background-color: gray;
9999
}
100+
101+
#example-invisible {
102+
display: none;
103+
}
104+
105+
#example-invisible[min-width~="100px"]{
106+
color: red;
107+
font-weight: bold;
108+
}
100109
</style>
101110

102111
<script type="text/javascript">
@@ -257,7 +266,7 @@ <h2>Demo 2</h2>
257266
</div>
258267
</div>
259268
<div class="code html">
260-
<textarea class="html">
269+
<textarea class="html" readonly>
261270
<div class="example-2">
262271
<h2>Demo 2</h2>
263272
<div class="example-2-box">
@@ -373,6 +382,26 @@ <h2>ResizeSensor Demo</h2>
373382
</div>
374383
</div>
375384

385+
<div class="example">
386+
<h2>Invisible Demo</h2>
387+
<div style="height: 80px">
388+
<div class="examplesResizerDemos">
389+
Press button to show.
390+
<div id="example-invisible">
391+
This should be red.
392+
</div>
393+
</div>
394+
</div>
395+
<div class="example-desc">
396+
<button onclick="document.getElementById('example-invisible').style.display = 'block'">Unhide box</button>
397+
<script type="text/javascript">
398+
new ResizeSensor(document.getElementById('example-invisible'), function(d) {
399+
console.log('invisible box changed', d);
400+
});
401+
</script>
402+
</div>
403+
</div>
404+
376405
<div class="example">
377406
<h2>Performance Demo</h2>
378407
<div class="dynamic">
@@ -393,4 +422,4 @@ <h2>Performance Demo</h2>
393422
</div>
394423
</div>
395424
</div>
396-
</body>
425+
</body>

0 commit comments

Comments
 (0)