forked from scottcorgan/angular-scrollto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-scrollto.js
43 lines (36 loc) · 1.59 KB
/
angular-scrollto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(function () {
'use strict';
angular.module('scrollto', [])
.provider('scrollToConfig', scrollToConfig)
.directive('scrollTo', scrollTo);
function scrollToConfig() {
this.config = {container: 'html, body', offset: 0, duration: 150};
this.$get = function () { return {config: this.config}; };
}
scrollTo.$inject = ['$timeout', 'scrollToConfig'];
function scrollTo($timeout, scrollToConfig) {
return {restrict: 'A', link: link};
function link(scope, element, attrs) {
var settings = angular.extend(
{scrollTo: angular.element(), easing: 'swing'},
scrollToConfig.config, attrs);
element.on('click', function () { $timeout(scroll(settings)); });
// Hook to trigger the scrolling action outside of the target element (e.g. can call from a Controller)
scope.triggerScroll = function(delay) {
delay = delay || 0; // delay is optional
$timeout(scroll(settings), delay);
};
}
function scroll (settings) {
return function () {
var scrollPane = angular.element(settings.container);
var scrollTo = (typeof settings.scrollTo == "number") ? settings.scrollTo : angular.element(settings.scrollTo);
if (typeof scrollTo.offset() == 'undefined') { return; } // element no longer available
var scrollY = (typeof scrollTo == "number") ? scrollTo : scrollTo.offset().top - settings.offset;
scrollPane.animate({scrollTop : scrollY }, settings.duration, settings.easing, function(){
if (typeof callback == 'function') { callback.call(this); }
});
}
}
}
})();