angular-scroll
Angular is only dependency (no jQuery). 8K minified or 2K gzipped.
Example
Check out the live demo or the source code.
Install
With bower:
$ bower install angular-scroll
With npm (for use with browserify):
$ npm install angular-scroll
You can also download the production version or the development version.
If you prefer a CDN hosted version (which might speed up your load times), check out cdnjs.com/libraries/angular-scroll.
Don't forget to add duScroll
to your module dependencies.
angular.element
Scroll API
This module extends the angular.element
object with a few jQuery like functions. Note that $document
is an angular.element
, for usage example see below. In case of name collisions existing jQuery or jqlite functions will be preserved, just use the prefixed version: ie .duScrollTo()
instead of .scrollTo()
.
.scrollTo( left, top [, duration [, easing ] ] )
Scrolls the element/window to the specified left/top position. If duration
is specified the scrolling is animated for n milliseconds. If easing
is ommited the animation will default to the duScrollEasing
function.
.scrollTo( element [, offset, [, duration [, easing ] ] ] )
Alias of .scrollToElement
.
.scrollToElement( element [, offset, [, duration [, easing ] ] ] )
Scrolls to the specified element, if offset
is passed it will be subtracted from the elements position which is good if one uses floating menus.
.scrollToElementAnimated( element [, offset, [, duration [, easing ] ] ] )
Convenience function. Works exactly the same as scrollToElement
but uses the default values from duScrollOffset
, duScrollDuration
and duScrollEasing
unless otherwise specified.
.scrollTop|scrollLeft( )
Returns current scroll position.
.scrollTop|scrollLeft( top [, duration [, easing ] ] )
Scrolls to specified position in either axis, with optional animation.
.scrollTopAnimated|scrollLeftAnimated( top [, duration [, easing ] ] )
Convenience function like scrollToElementAnimated
but for scrollTop
/scrollLeft
.
Promises
Animated scrolling returns a $q
promise, it will resolve when the scrolling has finished or be rejected if cancelled (by starting another scroll animation before it finished).
Example
angular ;
The above example can be achieved by configuration instead of arguments:
angular value'duScrollDuration' 2000 value'duScrollOffset' 30 ;
Directives
du-smooth-scroll
Provides smooth anchor scrolling.
Scroll it!
If you, for some reason, do not want to use the href
attribute as fallback, just use the du-smooth-scroll
attribute instead but without leading #. Example: <a du-smooth-scroll="anchor">
.
du-scrollspy
Observes whether the target element is at the top of the viewport (or container) and adds an active
class if so. Takes optional offset
and duration
attributes which is passed on to .scrollTo
,
Am i active?
or together with Bootstrap
Link
du-spy-context
Enables multiple sets of spies on the same target element. Takes optional offset
attribute to
Link Link
du-scroll-container
Modifies behavior of du-scrollspy
and du-smooth-scroll
to observe/scroll within and element instead of the window/document. Good for modals/elements with overflow: auto/scroll
.
This is the top Scroll to me, or the top
If your links lie outside of the scrollable element, wrap them with the du-scroll-container
directive and send the element id as argument:
Link [...]
All in together now
The directives play well together, try the demo or inspect its source code.
Link Link [...]
Observing Scroll Position
NOTE: the $duScrollChanged
event and the scrollPosition
service are deprecated. Use angular.element().on()
together with .scrollTop()
instead.
angular ;
Configuration
Scroll speed
Duration is defined in milliseconds.
To set a scroll duration on a single anchor:
Scroll it!
To change the default duration:
angularvalue'duScrollDuration' 5000;
Scroll easing
Set the duScrollEasing
value to a function that takes and returns a value between 0 to 1. Here's a few examples to choose from.
{ return 1-x;}angularvalue'duScrollEasing' invertedEasingFunction;
You can also pass a custom easing function as the fourth argument in scrollTo
.
Debounce Scroll Events
Set the duScrollSpyWait
value in milliseconds to debounce the handler and prevent it from triggering frequent events and increase performance for large pages and/or navigations with expanding nodes.
angularvalue'duScrollSpyWait' 1000;
Greedy option
Set the duScrollGreedy
value to true
if the elements you are observing are not wrapping the whole section you want to observe, but merely the first one in the section (such as headlines).
angularvalue'duScrollGreedy' true;
Offset
To change default offset (in pixels) for the du-smooth-scroll
directive:
angularvalue'duScrollOffset' 30;
When to cancel scroll animation
Specify on which events on the container the scroll animation should be cancelled by modifying duScrollCancelOnEvents
, set to false
to disable entirely as shown below. Defaults to scroll mousedown mousewheel touchmove keydown
.
angularvalue'duScrollCancelOnEvents' false;
Bottom spy
To make the last du-scrollspy
link active when scroll reaches page/container bottom:
angularvalue'duScrollBottomSpy' true;
Active class
Specify the active class name to apply to a link when it is active, default is active
.
angularvalue'duScrollActiveClass' 'custom-class';
Events
The duScrollspy
directive fires the global events duScrollspy:becameActive
and duScrollspy:becameInactive
with an angular.element wrapped element as first argument and the element being spied on as second. This is nice to have if you want the URL bar to reflect where on the page the visitor are, like this:
angular ;
Building
$ npm install
$ bower install
$ gulp
Tests
Unit tests
$ npm test
End to end tests
$ npm run update-webdriver
$ npm run protractor