@@ -232,7 +232,7 @@ ______________________________________________________________________________*/
232
232
233
233
sr . tools . forOwn ( sr . store . elements , function ( elemId ) {
234
234
elem = sr . store . elements [ elemId ] ;
235
- visible = sr . isVisible ( elem ) ;
235
+ visible = sr . isElemVisible ( elem ) ;
236
236
if ( visible && ! elem . revealed ) {
237
237
238
238
if ( elem . config . useDelay === 'always'
@@ -296,36 +296,89 @@ ______________________________________________________________________________*/
296
296
}
297
297
} ;
298
298
299
- ScrollReveal . prototype . isVisible = function ( elem ) {
300
- var config , rect , viewable ;
301
- var viewport = {
302
- width : window . document . documentElement . clientWidth ,
303
- height : window . document . documentElement . clientHeight
299
+ ScrollReveal . prototype . getContainerSize = function ( container ) {
300
+ if ( ! container ) {
301
+ container = window . document . documentElement ;
302
+ }
303
+ var w = container [ 'clientWidth' ] || 0 ;
304
+ var h = container [ 'clientHeight' ] || 0 ;
305
+ return {
306
+ width : w ,
307
+ height : h
304
308
} ;
305
- if ( elem . config . container ) {
306
- var container = elem . config . container . getBoundingClientRect ( ) ;
307
- viewable = {
308
- top : sr . tools . clamp ( 0 , container . top , viewport . height ) ,
309
- right : sr . tools . clamp ( 0 , container . right , viewport . width ) ,
310
- bottom : sr . tools . clamp ( 0 , container . bottom , viewport . height ) ,
311
- left : sr . tools . clamp ( 0 , container . left , viewport . width )
309
+ } ;
310
+
311
+ ScrollReveal . prototype . getScrolled = function ( container ) {
312
+ if ( ! container ) {
313
+ return {
314
+ x : window . pageXOffset ,
315
+ y : window . pageYOffset
312
316
} ;
313
317
} else {
314
- viewable = {
315
- top : 0 ,
316
- right : viewport . width ,
317
- bottom : viewport . height ,
318
- left : 0
318
+ var offset = sr . getOffset ( container ) ;
319
+ return {
320
+ x : container . scrollLeft + offset . left ,
321
+ y : container . scrollTop + offset . top
322
+ } ;
323
+ }
324
+ } ;
325
+
326
+ ScrollReveal . prototype . getOffset = function ( domEl ) {
327
+ var offsetTop = 0 ;
328
+ var offsetLeft = 0 ;
329
+
330
+ do {
331
+ if ( ! isNaN ( domEl . offsetTop ) ) {
332
+ offsetTop += domEl . offsetTop ;
333
+ }
334
+ if ( ! isNaN ( domEl . offsetLeft ) ) {
335
+ offsetLeft += domEl . offsetLeft ;
319
336
}
337
+ } while ( domEl = domEl . offsetParent ) ;
338
+
339
+ return {
340
+ top : offsetTop ,
341
+ left : offsetLeft
342
+ } ;
343
+ } ;
344
+
345
+ ScrollReveal . prototype . isElemVisible = function ( elem ) {
346
+
347
+ var offset = sr . getOffset ( elem . domEl ) ;
348
+ var container = sr . getContainerSize ( elem . config . container ) ;
349
+ var scrolled = sr . getScrolled ( elem . config . container ) ;
350
+ var vF = elem . config . viewFactor ;
351
+
352
+ var elemHeight = elem . domEl . offsetHeight ;
353
+ var elemWidth = elem . domEl . offsetWidth ;
354
+ var elemTop = offset . top ;
355
+ var elemBottom = elemTop + elemHeight ;
356
+ var elemLeft = offset . left ;
357
+ var elemRight = elemLeft + elemWidth ;
358
+
359
+ return ( confirmBounds ( ) || isPositionFixed ( ) ) ;
360
+
361
+ function confirmBounds ( ) {
362
+
363
+ var top = elemTop + elemHeight * vF ;
364
+ var bottom = elemBottom - elemHeight * vF ;
365
+ var left = elemLeft + elemWidth * vF ;
366
+ var right = elemRight - elemWidth * vF ;
367
+
368
+ var viewTop = scrolled . y + elem . config . viewOffset . top ;
369
+ var viewBottom = scrolled . y - elem . config . viewOffset . bottom + container . height ;
370
+ var viewLeft = scrolled . x + elem . config . viewOffset . left ;
371
+ var viewRight = scrolled . x - elem . config . viewOffset . right + container . width ;
372
+
373
+ return ( top < viewBottom )
374
+ && ( bottom > viewTop )
375
+ && ( left > viewLeft )
376
+ && ( right < viewRight ) ;
377
+ }
378
+
379
+ function isPositionFixed ( ) {
380
+ return ( window . getComputedStyle ( elem . domEl ) . position === 'fixed' ) ;
320
381
}
321
- rect = elem . domEl . getBoundingClientRect ( ) ;
322
- config = elem . config ;
323
- return (
324
- rect . top + ( rect . height * config . viewFactor ) < viewable . bottom - config . viewOffset . bottom &&
325
- rect . right - ( rect . width * config . viewFactor ) > viewable . left + config . viewOffset . left &&
326
- rect . bottom - ( rect . height * config . viewFactor ) > viewable . top + config . viewOffset . top &&
327
- rect . left + ( rect . width * config . viewFactor ) < viewable . right - config . viewOffset . right
328
- ) ;
329
382
} ;
330
383
331
384
ScrollReveal . prototype . sync = function ( ) {
@@ -342,10 +395,6 @@ ______________________________________________________________________________*/
342
395
343
396
var Tools = ( function ( ) {
344
397
345
- Tools . prototype . clamp = function ( min , num , max ) {
346
- return Math . min ( Math . max ( min , num ) , max ) ;
347
- } ;
348
-
349
398
Tools . prototype . isObject = function ( object ) {
350
399
return object !== null && typeof object === 'object' && object . constructor == Object ;
351
400
} ;
0 commit comments