|
36 | 36 | return globalWindow.setTimeout(fn, 20); |
37 | 37 | }; |
38 | 38 |
|
| 39 | + var cancelAnimationFrame = globalWindow.cancelAnimationFrame || |
| 40 | + globalWindow.mozCancelAnimationFrame || |
| 41 | + globalWindow.webkitCancelAnimationFrame || |
| 42 | + function (timer) { |
| 43 | + globalWindow.clearTimeout(timer); |
| 44 | + }; |
| 45 | + |
39 | 46 | /** |
40 | 47 | * Iterate over each of the provided element(s). |
41 | 48 | * |
|
102 | 109 | * @constructor |
103 | 110 | */ |
104 | 111 | 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 | + |
107 | 115 | /** |
108 | 116 | * |
109 | 117 | * @constructor |
|
201 | 209 | element.style.position = 'relative'; |
202 | 210 | } |
203 | 211 |
|
204 | | - var dirty, rafId; |
| 212 | + var dirty = false; |
| 213 | + |
| 214 | + //last request animation frame id used in onscroll event |
| 215 | + var rafId = 0; |
205 | 216 | var size = getElementSize(element); |
206 | 217 | var lastWidth = 0; |
207 | 218 | var lastHeight = 0; |
208 | 219 | var initialHiddenCheck = true; |
209 | | - lastAnimationFrame = 0; |
| 220 | + lastAnimationFrameForInvisibleCheck = 0; |
210 | 221 |
|
211 | 222 | var resetExpandShrink = function () { |
212 | 223 | var width = element.offsetWidth; |
|
228 | 239 | var invisible = element.offsetWidth === 0 && element.offsetHeight === 0; |
229 | 240 | if (invisible) { |
230 | 241 | // 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; |
235 | 245 | reset(); |
236 | 246 | }); |
237 | 247 | } |
|
282 | 292 | addEvent(expand, 'scroll', onScroll); |
283 | 293 | addEvent(shrink, 'scroll', onScroll); |
284 | 294 |
|
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 | + }); |
287 | 300 | } |
288 | 301 |
|
289 | 302 | forEachElement(element, function(elem){ |
|
292 | 305 |
|
293 | 306 | this.detach = function(ev) { |
294 | 307 | // 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; |
298 | 311 | } |
299 | 312 | ResizeSensor.detach(element, ev); |
300 | 313 | }; |
|
0 commit comments