forked from d-oliveros/ngSmoothScroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-smooth-scroll-1.6.0.js
161 lines (134 loc) · 5.02 KB
/
angular-smooth-scroll-1.6.0.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* =============================================================
/*
/* Angular Smooth Scroll 1.6.0
/* Animates scrolling to elements, by David Oliveros.
/*
/*
/* Easing support contributed by Willem Liu.
/* https://github.com/willemliu
/*
/* Easing functions forked from Gaëtan Renaudeau.
/* https://gist.github.com/gre/1650294
/*
/* Infinite loop bugs in iOS and Chrome (when zoomed) by Alex Guzman.
/* https://github.com/alexguzman
/*
/* Influenced by Chris Ferdinandi
/* https://github.com/cferdinandi
/*
/*
/* Free to use under the MIT License.
/*
/* ============================================================= */
angular.module('smoothScroll', [])
// Scrolls the window to this element, optionally validating an expression
//
.directive('smoothScroll', ['$timeout', 'smoothScroll', function($timeout, smoothScroll){
return {
restrict: 'A',
link: function($scope, $elem, $attrs){
$timeout(function(){
if ( typeof $attrs.scrollIf === 'undefined' || $attrs.scrollIf === 'true' ){
smoothScroll($elem[0], {
duration: $attrs.duration,
offset: $attrs.offset,
easing: $attrs.easing,
});
}
});
}
};
}])
// Scrolls to a specified element ID when this element is clicked
//
.directive('scrollTo', ['smoothScroll', function(smoothScroll){
return {
restrict: 'A',
link: function($scope, $elem, $attrs){
var targetElement;
$elem.on('click', function(e){
targetElement = document.getElementById($attrs.scrollTo);
if ( targetElement ) {
e.preventDefault();
smoothScroll(targetElement, {
duration: $attrs.duration,
offset: $attrs.offset,
easing: $attrs.easing,
});
return false;
}
});
}
};
}])
// Smooth scrolls the window to the provided element.
//
.factory('smoothScroll', ['$timeout', function($timeout){
var smoothScroll = function (element, options) {
$timeout(function(){
var startLocation = window.pageYOffset,
timeLapsed = 0,
percentage, position;
// Options
//
options = options || {};
var duration = options.duration || 800,
offset = options.offset || 0,
easing = options.easing || 'easeInOutQuart';
// Calculate the easing pattern
//
var easingPattern = function (type, time) {
if ( type == 'easeInQuad' ) return time * time; // accelerating from zero velocity
if ( type == 'easeOutQuad' ) return time * (2 - time); // decelerating to zero velocity
if ( type == 'easeInOutQuad' ) return time < 0.5 ? 2 * time * time : -1 + (4 - 2 * time) * time; // acceleration until halfway, then deceleration
if ( type == 'easeInCubic' ) return time * time * time; // accelerating from zero velocity
if ( type == 'easeOutCubic' ) return (--time) * time * time + 1; // decelerating to zero velocity
if ( type == 'easeInOutCubic' ) return time < 0.5 ? 4 * time * time * time : (time - 1) * (2 * time - 2) * (2 * time - 2) + 1; // acceleration until halfway, then deceleration
if ( type == 'easeInQuart' ) return time * time * time * time; // accelerating from zero velocity
if ( type == 'easeOutQuart' ) return 1 - (--time) * time * time * time; // decelerating to zero velocity
if ( type == 'easeInOutQuart' ) return time < 0.5 ? 8 * time * time * time * time : 1 - 8 * (--time) * time * time * time; // acceleration until halfway, then deceleration
if ( type == 'easeInQuint' ) return time * time * time * time * time; // accelerating from zero velocity
if ( type == 'easeOutQuint' ) return 1 + (--time) * time * time * time * time; // decelerating to zero velocity
if ( type == 'easeInOutQuint' ) return time < 0.5 ? 16 * time * time * time * time * time : 1 + 16 * (--time) * time * time * time * time; // acceleration until halfway, then deceleration
return time; // no easing, no acceleration
};
// Calculate how far to scroll
//
var getEndLocation = function (element) {
var location = 0;
if (element.offsetParent) {
do {
location += element.offsetTop;
element = element.offsetParent;
} while (element);
}
location = Math.max(location - offset, 0);
return location;
};
var endLocation = getEndLocation(element);
var distance = endLocation - startLocation;
// Stop the scrolling animation when the anchor is reached (or at the top/bottom of the page)
//
var stopAnimation = function () {
var currentLocation = window.pageYOffset;
if ( position == endLocation || currentLocation == endLocation || ( (window.innerHeight + currentLocation) >= document.body.scrollHeight ) ) {
clearInterval(runAnimation);
}
};
// Scroll the page by an increment, and check if it's time to stop
//
var animateScroll = function () {
timeLapsed += 16;
percentage = ( timeLapsed / duration );
percentage = ( percentage > 1 ) ? 1 : percentage;
position = startLocation + ( distance * easingPattern(easing, percentage) );
window.scrollTo( 0, position );
stopAnimation();
};
// Init
//
var runAnimation = setInterval(animateScroll, 16);
});
};
return smoothScroll;
}]);