@@ -11,17 +11,17 @@ var duScrollDefaultEasing = function (x) {
1111} ;
1212
1313angular . module ( 'duScroll' , [
14- 'duScroll.scrollspy' ,
15- 'duScroll.smoothScroll' ,
16- 'duScroll.scrollContainer' ,
14+ 'duScroll.scrollspy' ,
15+ 'duScroll.smoothScroll' ,
16+ 'duScroll.scrollContainer' ,
1717 'duScroll.spyContext' ,
1818 'duScroll.scrollHelpers'
1919] )
2020 //Default animation duration for smoothScroll directive
2121 . value ( 'duScrollDuration' , 350 )
2222 //Scrollspy debounce interval, set to 0 to disable
2323 . value ( 'duScrollSpyWait' , 100 )
24- //Wether or not multiple scrollspies can be active at once
24+ //Wether or not multiple scrollspies can be active at once
2525 . value ( 'duScrollGreedy' , false )
2626 //Default offset for smoothScroll directive
2727 . value ( 'duScrollOffset' , 0 )
@@ -51,7 +51,7 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
5151 var aliasFn ;
5252 if ( angular . isElement ( left ) ) {
5353 aliasFn = this . duScrollToElement ;
54- } else if ( duration ) {
54+ } else if ( angular . isDefined ( duration ) ) {
5555 aliasFn = this . duScrollToAnimated ;
5656 }
5757 if ( aliasFn ) {
@@ -93,7 +93,10 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
9393 }
9494 deferred = $q . defer ( ) ;
9595
96- if ( ! deltaLeft && ! deltaTop ) {
96+ if ( duration === 0 || ( ! deltaLeft && ! deltaTop ) ) {
97+ if ( duration === 0 ) {
98+ el . duScrollTo ( left , top ) ;
99+ }
97100 deferred . resolve ( ) ;
98101 return deferred . promise ;
99102 }
@@ -221,7 +224,7 @@ angular.module('duScroll.requestAnimation', ['duScroll.polyfill'])
221224 lastTime = currTime + timeToCall ;
222225 return id ;
223226 } ;
224-
227+
225228 return polyfill ( 'requestAnimationFrame' , fallback ) ;
226229} ] )
227230. factory ( 'cancelAnimation' , [ "polyfill" , "$timeout" , function ( polyfill , $timeout ) {
@@ -236,7 +239,7 @@ angular.module('duScroll.requestAnimation', ['duScroll.polyfill'])
236239
237240
238241angular . module ( 'duScroll.spyAPI' , [ 'duScroll.scrollContainerAPI' ] )
239- . factory ( 'spyAPI' , [ "$rootScope" , "$timeout" , "scrollContainerAPI" , "duScrollGreedy" , "duScrollSpyWait" , function ( $rootScope , $timeout , scrollContainerAPI , duScrollGreedy , duScrollSpyWait ) {
242+ . factory ( 'spyAPI' , [ "$rootScope" , "$timeout" , "$window" , "$document" , " scrollContainerAPI", "duScrollGreedy" , "duScrollSpyWait" , function ( $rootScope , $timeout , $window , $document , scrollContainerAPI , duScrollGreedy , duScrollSpyWait ) {
240243 'use strict' ;
241244
242245 var createScrollHandler = function ( context ) {
@@ -245,11 +248,16 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
245248 queued = false ;
246249 var container = context . container ,
247250 containerEl = container [ 0 ] ,
248- containerOffset = 0 ;
251+ containerOffset = 0 ,
252+ bottomReached ;
249253
250254 if ( typeof HTMLElement !== 'undefined' && containerEl instanceof HTMLElement || containerEl . nodeType && containerEl . nodeType === containerEl . ELEMENT_NODE ) {
251255 containerOffset = containerEl . getBoundingClientRect ( ) . top ;
256+ bottomReached = Math . round ( containerEl . scrollTop + containerEl . clientHeight ) >= containerEl . scrollHeight ;
257+ } else {
258+ bottomReached = Math . round ( $window . pageYOffset + $window . innerHeight ) >= $document [ 0 ] . body . scrollHeight ;
252259 }
260+ var compareProperty = ( bottomReached ? 'bottom' : 'top' ) ;
253261
254262 var i , currentlyActive , toBeActive , spies , spy , pos ;
255263 spies = context . spies ;
@@ -261,15 +269,17 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
261269 pos = spy . getTargetPosition ( ) ;
262270 if ( ! pos ) continue ;
263271
264- if ( pos . top + spy . offset - containerOffset < 20 && ( pos . top * - 1 + containerOffset ) < pos . height ) {
265- if ( ! toBeActive || toBeActive . top < pos . top ) {
272+ if ( bottomReached || ( pos . top + spy . offset - containerOffset < 20 && ( duScrollGreedy || pos . top * - 1 + containerOffset ) < pos . height ) ) {
273+ //Find the one closest the viewport top or the page bottom if it's reached
274+ if ( ! toBeActive || toBeActive [ compareProperty ] < pos [ compareProperty ] ) {
266275 toBeActive = {
267- top : pos . top ,
268276 spy : spy
269277 } ;
278+ toBeActive [ compareProperty ] = pos [ compareProperty ] ;
270279 }
271280 }
272281 }
282+
273283 if ( toBeActive ) {
274284 toBeActive = toBeActive . spy ;
275285 }
@@ -438,8 +448,8 @@ angular.module('duScroll.scrollContainerAPI', [])
438448 } ;
439449
440450 return {
441- getContainerId : getContainerId ,
442- getContainer : getContainer ,
451+ getContainerId : getContainerId ,
452+ getContainer : getContainer ,
443453 setContainer : setContainer ,
444454 removeContainer : removeContainer
445455 } ;
@@ -457,7 +467,7 @@ angular.module('duScroll.smoothScroll', ['duScroll.scrollHelpers', 'duScroll.scr
457467
458468 var target = document . getElementById ( $attr . href . replace ( / .* (? = # [ ^ \s ] + $ ) / , '' ) . substring ( 1 ) ) ;
459469 if ( ! target || ! target . getBoundingClientRect ) return ;
460-
470+
461471 if ( e . stopPropagation ) e . stopPropagation ( ) ;
462472 if ( e . preventDefault ) e . preventDefault ( ) ;
463473
@@ -466,8 +476,8 @@ angular.module('duScroll.smoothScroll', ['duScroll.scrollHelpers', 'duScroll.scr
466476 var container = scrollContainerAPI . getContainer ( $scope ) ;
467477
468478 container . duScrollToElement (
469- angular . element ( target ) ,
470- isNaN ( offset ) ? 0 : offset ,
479+ angular . element ( target ) ,
480+ isNaN ( offset ) ? 0 : offset ,
471481 isNaN ( duration ) ? 0 : duration
472482 ) ;
473483 } ) ;
0 commit comments