UNPKG

10.1 kBMarkdownView Raw
1angular-scroll
2==============
3
4Angular is only dependency (no jQuery). 8K minified or 2K gzipped.
5
6Example
7-------
8Check 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
10Install
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
21You 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
23If 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
26Don't forget to add `duScroll` to your module dependencies.
27
28`angular.element` Scroll API
29----------------------------
30
31This 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 ] ] )`
34Scrolls 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 ] ] ] )`
37Alias of `.scrollToElement`.
38
39#### `.scrollToElement( element [, offset, [, duration [, easing ] ] ] )`
40Scrolls 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 ] ] ] )`
43Convenience function. Works exactly the same as `scrollToElement` but uses the default values from `duScrollOffset`, `duScrollDuration` and `duScrollEasing` unless otherwise specified.
44
45#### `.scrollTop|scrollLeft( )`
46Returns current scroll position.
47
48#### `.scrollTop|scrollLeft( top [, duration [, easing ] ] )`
49Scrolls to specified position in either axis, with optional animation.
50
51#### `.scrollTopAnimated|scrollLeftAnimated( top [, duration [, easing ] ] )`
52Convenience function like `scrollToElementAnimated` but for `scrollTop`/`scrollLeft`.
53
54#### Promises
55Animated 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
59angular.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
78The above example can be achieved by configuration instead of arguments:
79
80```js
81angular.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
96Directives
97----------
98
99### `du-smooth-scroll`
100Provides smooth anchor scrolling.
101```html
102<a href="#anchor" du-smooth-scroll>Scroll it!</a>
103```
104
105If 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">`.
106
107### `du-scrollspy`
108Observes 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`,
109
110```html
111<a href="#anchor" du-scrollspy>Am i active?</a>
112```
113
114or together with Bootstrap
115
116```html
117<ul class="nav navbar-nav">
118 <li du-scrollspy="anchor"><a href="#anchor">Link</a></li>
119</ul>
120```
121
122### `du-spy-context`
123Enables multiple sets of spies on the same target element. Takes optional `offset` attribute to
124
125```html
126<ul du-spy-context class="nav navbar-nav">
127 <li du-scrollspy="anchor"><a href="#anchor">Link</a></li>
128</ul>
129<ul du-spy-context class="nav navbar-nav">
130 <li du-scrollspy="anchor"><a href="#anchor">Link</a></li>
131</ul>
132```
133### `du-scroll-container`
134Modifies 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`.
135
136```html
137<div du-scroll-container>
138 <p id="top">This is the top</p>
139 <p id="anchor">Scroll to me, or <a href="#top" du-smooth-scroll>the top</a></p>
140</div>
141```
142
143If your links lie outside of the scrollable element, wrap them with the `du-scroll-container` directive and send the element id as argument:
144
145```html
146<ul du-scroll-container="scroll-container">
147 <li><a href="#anchor" du-smooth-scroll>Link</a></li>
148</ul>
149<div id="scroll-container">
150 [...]
151</div>
152```
153
154### [All in together now](http://www.youtube.com/watch?v=cx4KtTezEFg&feature=kp)
155The 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).
156
157```html
158<ul du-spy-context du-scroll-container="scroll-container">
159 <li><a href="#anchor" offset="30" du-smooth-scroll du-scrollspy>Link</a></li>
160</ul>
161<ul du-spy-context du-scroll-container="scroll-container">
162 <li><a href="#anchor" offset="30" du-smooth-scroll du-scrollspy>Link</a></li>
163</ul>
164<div id="scroll-container">
165 [...]
166</div>
167```
168
169Observing Scroll Position
170-------------------------
171
172**NOTE:** the `$duScrollChanged` event and the `scrollPosition` service are deprecated. Use `angular.element().on()` together with `.scrollTop()` instead.
173
174```js
175angular.module('myApp', ['duScroll']).
176 controller('MyCtrl', function($scope, $document){
177 $document.on('scroll', function() {
178 console.log('Document scrolled to ', $document.scrollLeft(), $document.scrollTop());
179 });
180 var container = angular.element(document.getElementById('container'));
181 container.on('scroll', function() {
182 console.log('Container scrolled to ', container.scrollLeft(), container.scrollTop());
183 });
184 }
185);
186```
187
188Configuration
189-------------
190
191### Scroll speed
192Duration is defined in milliseconds.
193
194To set a scroll duration on a single anchor:
195```html
196<a href="#anchor" du-smooth-scroll duration="5000">Scroll it!</a>
197```
198
199To change the default duration:
200```js
201angular.module('myApp', ['duScroll']).value('duScrollDuration', 5000);
202```
203
204### Scroll easing
205Set 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.
206
207```js
208function invertedEasingFunction(x) {
209 return 1-x;
210}
211angular.module('myApp', ['duScroll']).value('duScrollEasing', invertedEasingFunction);
212```
213
214You can also pass a custom easing function as the fourth argument in `scrollTo`.
215
216### Debounce Scroll Events
217Set 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.
218
219```js
220angular.module('myApp', ['duScroll']).value('duScrollSpyWait', 1000);
221```
222
223### Greedy option
224Set 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).
225
226```js
227angular.module('myApp', ['duScroll']).value('duScrollGreedy', true);
228```
229
230### Offset
231To change default offset (in pixels) for the `du-smooth-scroll` directive:
232
233```js
234angular.module('myApp', ['duScroll']).value('duScrollOffset', 30);
235```
236
237### When to cancel scroll animation
238Specify 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`.
239
240```js
241angular.module('myApp', ['duScroll']).value('duScrollCancelOnEvents', false);
242```
243
244### Bottom spy
245To make the last `du-scrollspy` link active when scroll reaches page/container bottom:
246
247```js
248angular.module('myApp', ['duScroll']).value('duScrollBottomSpy', true);
249```
250
251### Active class
252Specify the active class name to apply to a link when it is active, default is `active`.
253
254```js
255angular.module('myApp', ['duScroll']).value('duScrollActiveClass', 'custom-class');
256```
257
258Events
259------
260
261The `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:
262
263```js
264angular.module('myApp', ['duScroll']).
265 run(function($rootScope) {
266 if(!window.history || !history.replaceState) {
267 return;
268 }
269 $rootScope.$on('duScrollspy:becameActive', function($event, $element, $target){
270 //Automaticly update location
271 var hash = $element.prop('hash');
272 if (hash) {
273 history.replaceState(null, null, hash);
274 }
275 });
276 });
277```
278
279
280Building
281--------
282
283 $ npm install
284 $ bower install
285 $ gulp
286
287Tests
288-----
289
290### Unit tests
291
292 $ npm test
293
294### End to end tests
295
296 $ npm run update-webdriver
297 $ npm run protractor