|
| 1 | +angular-scroll |
| 2 | +============== |
| 3 | + |
| 4 | +Angular is only dependency (no jQuery). 8K minified or 2K gzipped. |
| 5 | + |
| 6 | +Example |
| 7 | +------- |
| 8 | +Check out [the live demo](http://oblador.github.io/angular-scroll/) or the [source code](https://github.com/oblador/angular-scroll/blob/master/example/index.html). |
| 9 | + |
| 10 | +Install |
| 11 | +------- |
| 12 | + |
| 13 | +#### With bower: |
| 14 | + |
| 15 | + $ bower install angular-scroll |
| 16 | + |
| 17 | +#### With npm (for use with browserify): |
| 18 | + |
| 19 | + $ npm install angular-scroll |
| 20 | + |
| 21 | +You can also download the [production version](https://raw.github.com/oblador/angular-scroll/master/angular-scroll.min.js)or the [development version](https://raw.github.com/oblador/angular-scroll/master/angular-scroll.js). |
| 22 | + |
| 23 | +If you prefer a CDN hosted version (which might speed up your load times), check out [cdnjs.com/libraries/angular-scroll](https://cdnjs.com/libraries/angular-scroll). |
| 24 | + |
| 25 | + |
| 26 | +Don't forget to add `duScroll` to your module dependencies. |
| 27 | + |
| 28 | +`angular.element` Scroll API |
| 29 | +---------------------------- |
| 30 | + |
| 31 | +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()`. |
| 32 | + |
| 33 | +#### `.scrollTo( left, top [, duration [, easing ] ] )` |
| 34 | +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. |
| 35 | + |
| 36 | +#### `.scrollTo( element [, offset, [, duration [, easing ] ] ] )` |
| 37 | +Alias of `.scrollToElement`. |
| 38 | + |
| 39 | +#### `.scrollToElement( element [, offset, [, duration [, easing ] ] ] )` |
| 40 | +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. |
| 41 | + |
| 42 | +#### `.scrollToElementAnimated( element [, offset, [, duration [, easing ] ] ] )` |
| 43 | +Convenience function. Works exactly the same as `scrollToElement` but uses the default values from `duScrollOffset`, `duScrollDuration` and `duScrollEasing` unless otherwise specified. |
| 44 | + |
| 45 | +#### `.scrollTop|scrollLeft( )` |
| 46 | +Returns current scroll position. |
| 47 | + |
| 48 | +#### `.scrollTop|scrollLeft( top [, duration [, easing ] ] )` |
| 49 | +Scrolls to specified position in either axis, with optional animation. |
| 50 | + |
| 51 | +#### `.scrollTopAnimated|scrollLeftAnimated( top [, duration [, easing ] ] )` |
| 52 | +Convenience function like `scrollToElementAnimated` but for `scrollTop`/`scrollLeft`. |
| 53 | + |
| 54 | +#### Promises |
| 55 | +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). |
| 56 | + |
| 57 | +#### Example |
| 58 | +```js |
| 59 | +angular.module('myApp', ['duScroll']). |
| 60 | + controller('myCtrl', function($scope, $document) { |
| 61 | + var top = 400; |
| 62 | + var duration = 2000; //milliseconds |
| 63 | + |
| 64 | + //Scroll to the exact position |
| 65 | + $document.scrollTop(top, duration).then(function() { |
| 66 | + console && console.log('You just scrolled to the top!'); |
| 67 | + }); |
| 68 | + |
| 69 | + var offset = 30; //pixels; adjust for floating menu, context etc |
| 70 | + //Scroll to #some-id with 30 px "padding" |
| 71 | + //Note: Use this in a directive, not with document.getElementById |
| 72 | + var someElement = angular.element(document.getElementById('some-id')); |
| 73 | + $document.scrollToElement(someElement, offset, duration); |
| 74 | + } |
| 75 | +); |
| 76 | +``` |
| 77 | + |
| 78 | +The above example can be achieved by configuration instead of arguments: |
| 79 | + |
| 80 | +```js |
| 81 | +angular.module('myApp', ['duScroll']) |
| 82 | + .value('duScrollDuration', 2000) |
| 83 | + .value('duScrollOffset', 30) |
| 84 | + .controller('myCtrl', function($scope, $document) { |
| 85 | + $document.scrollTopAnimated(400).then(function() { |
| 86 | + console && console.log('You just scrolled to the top!'); |
| 87 | + }); |
| 88 | + |
| 89 | + var someElement = angular.element(document.getElementById('some-id')); |
| 90 | + $document.scrollToElementAnimated(someElement); |
| 91 | + } |
| 92 | +); |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +Directives |
| 97 | +---------- |
| 98 | + |
| 99 | +### `du-smooth-scroll` |
| 100 | +Provides smooth anchor scrolling. |
| 101 | +```html |
| 102 | +<a href="#anchor" du-smooth-scroll>Scroll it!</a> |
| 103 | +``` |
| 104 | + |
| 105 | +### `du-scrollspy` |
| 106 | +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`, |
| 107 | + |
| 108 | +```html |
| 109 | +<a href="#anchor" du-scrollspy>Am i active?</a> |
| 110 | +``` |
| 111 | + |
| 112 | +or together with Bootstrap |
| 113 | + |
| 114 | +```html |
| 115 | +<ul class="nav navbar-nav"> |
| 116 | + <li du-scrollspy="anchor"><a href="#anchor">Link</a></li> |
| 117 | +</ul> |
| 118 | +``` |
| 119 | + |
| 120 | +### `du-spy-context` |
| 121 | +Enables multiple sets of spies on the same target element. Takes optional `offset` attribute to |
| 122 | + |
| 123 | +```html |
| 124 | +<ul du-spy-context class="nav navbar-nav"> |
| 125 | + <li du-scrollspy="anchor"><a href="#anchor">Link</a></li> |
| 126 | +</ul> |
| 127 | +<ul du-spy-context class="nav navbar-nav"> |
| 128 | + <li du-scrollspy="anchor"><a href="#anchor">Link</a></li> |
| 129 | +</ul> |
| 130 | +``` |
| 131 | +### `du-scroll-container` |
| 132 | +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`. |
| 133 | + |
| 134 | +```html |
| 135 | +<div du-scroll-container> |
| 136 | + <p id="top">This is the top</p> |
| 137 | + <p id="anchor">Scroll to me, or <a href="#top" du-smooth-scroll>the top</a></p> |
| 138 | +</div> |
| 139 | +``` |
| 140 | + |
| 141 | +If your links lie outside of the scrollable element, wrap them with the `du-scroll-container` directive and send the element id as argument: |
| 142 | + |
| 143 | +```html |
| 144 | +<ul du-scroll-container="scroll-container"> |
| 145 | + <li><a href="#anchor" du-smooth-scroll>Link</a></li> |
| 146 | +</ul> |
| 147 | +<div id="scroll-container"> |
| 148 | + [...] |
| 149 | +</div> |
| 150 | +``` |
| 151 | + |
| 152 | +### [All in together now](http://www.youtube.com/watch?v=cx4KtTezEFg&feature=kp) |
| 153 | +The directives play well together, try [the demo](http://oblador.github.io/angular-scroll/container.html) or inspect its [source code](https://github.com/oblador/angular-scroll/blob/master/example/container.html). |
| 154 | + |
| 155 | +```html |
| 156 | +<ul du-spy-context du-scroll-container="scroll-container"> |
| 157 | + <li><a href="#anchor" offset="30" du-smooth-scroll du-scrollspy>Link</a></li> |
| 158 | +</ul> |
| 159 | +<ul du-spy-context du-scroll-container="scroll-container"> |
| 160 | + <li><a href="#anchor" offset="30" du-smooth-scroll du-scrollspy>Link</a></li> |
| 161 | +</ul> |
| 162 | +<div id="scroll-container"> |
| 163 | + [...] |
| 164 | +</div> |
| 165 | +``` |
| 166 | + |
| 167 | +Observing Scroll Position |
| 168 | +------------------------- |
| 169 | + |
| 170 | +**NOTE:** the `$duScrollChanged` event and the `scrollPosition` service are deprecated. Use `angular.element().on()` together with `.scrollTop()` instead. |
| 171 | + |
| 172 | +```js |
| 173 | +angular.module('myApp', ['duScroll']). |
| 174 | + controller('MyCtrl', function($scope, $document){ |
| 175 | + $document.on('scroll', function() { |
| 176 | + console.log('Document scrolled to ', $document.scrollLeft(), $document.scrollTop()); |
| 177 | + }); |
| 178 | + var container = angular.element(document.getElementById('container')); |
| 179 | + container.on('scroll', function() { |
| 180 | + console.log('Container scrolled to ', container.scrollLeft(), container.scrollTop()); |
| 181 | + }); |
| 182 | + } |
| 183 | +); |
| 184 | +``` |
| 185 | + |
| 186 | +Configuration |
| 187 | +------------- |
| 188 | + |
| 189 | +### Scroll speed |
| 190 | +Duration is defined in milliseconds. |
| 191 | + |
| 192 | +To set a scroll duration on a single anchor: |
| 193 | +```html |
| 194 | +<a href="#anchor" du-smooth-scroll duration="5000">Scroll it!</a> |
| 195 | +``` |
| 196 | + |
| 197 | +To change the default duration: |
| 198 | +```js |
| 199 | +angular.module('myApp', ['duScroll']).value('duScrollDuration', 5000); |
| 200 | +``` |
| 201 | + |
| 202 | +### Scroll easing |
| 203 | +Set the `duScrollEasing` value to a function that takes and returns a value between 0 to 1. Here's [a few examples](https://gist.github.com/gre/1650294) to choose from. |
| 204 | + |
| 205 | +```js |
| 206 | +function invertedEasingFunction(x) { |
| 207 | + return 1-x; |
| 208 | +} |
| 209 | +angular.module('myApp', ['duScroll']).value('duScrollEasing', invertedEasingFunction); |
| 210 | +``` |
| 211 | + |
| 212 | +You can also pass a custom easing function as the fourth argument in `scrollTo`. |
| 213 | + |
| 214 | +### Greedy option |
| 215 | +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). |
| 216 | + |
| 217 | +```js |
| 218 | +angular.module('myApp', ['duScroll']).value('duScrollGreedy', true); |
| 219 | +``` |
| 220 | + |
| 221 | +### Offset |
| 222 | +To change default offset (in pixels) for the `du-smooth-scroll` directive: |
| 223 | + |
| 224 | +```js |
| 225 | +angular.module('myApp', ['duScroll']).value('duScrollOffset', 30); |
| 226 | +``` |
| 227 | + |
| 228 | +### Bottom spy |
| 229 | +To make the last `du-scrollspy` link active when scroll reaches page/container bottom: |
| 230 | + |
| 231 | +```js |
| 232 | +angular.module('myApp', ['duScroll']).value('duScrollBottomSpy', true); |
| 233 | +``` |
| 234 | + |
| 235 | +Events |
| 236 | +------ |
| 237 | + |
| 238 | +The `duScrollspy` directive fires the global events `duScrollspy:becameActive` and `duScrollspy:becameInactive` with an angular.element wrapped element as first argument. This is nice to have if you want the URL bar to reflect where on the page the visitor are, like this: |
| 239 | + |
| 240 | +```js |
| 241 | +angular.module('myApp', ['duScroll']). |
| 242 | + config(function($locationProvider) { |
| 243 | + $locationProvider.html5Mode(true); |
| 244 | + }). |
| 245 | + run(function($rootScope, $location){ |
| 246 | + $rootScope.$on('duScrollspy:becameActive', function($event, $element){ |
| 247 | + //Automaticly update location |
| 248 | + var hash = $element.prop('hash'); |
| 249 | + if(hash) { |
| 250 | + $location.hash(hash.substr(1)).replace(); |
| 251 | + $rootScope.$apply(); |
| 252 | + } |
| 253 | + }); |
| 254 | + }); |
| 255 | +``` |
| 256 | + |
| 257 | + |
| 258 | +Building |
| 259 | +-------- |
| 260 | + |
| 261 | + $ gulp |
| 262 | + |
| 263 | +Tests |
| 264 | +----- |
| 265 | + |
| 266 | +### Unit tests |
| 267 | + |
| 268 | + $ npm test |
| 269 | + |
| 270 | +### End to end tests |
| 271 | + |
| 272 | + $ npm run update-webdriver |
| 273 | + $ npm run protractor |
0 commit comments