forked from d-oliveros/ngSmoothScroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-smooth-scroll.js
More file actions
215 lines (187 loc) · 6.65 KB
/
angular-smooth-scroll.js
File metadata and controls
215 lines (187 loc) · 6.65 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* =============================================================
/*
/* Angular Smooth Scroll 1.7.0
/* Animates scrolling to elements, by David Oliveros.
/*
/* Callback hooks contributed by Ben Armston
/* https://github.com/benarmston
/*
/* 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',
scope: {
callbackBefore: '&',
callbackAfter: '&',
},
link: function($scope, $elem, $attrs){
$timeout(function(){
if ( typeof $attrs.scrollIf === 'undefined' || $attrs.scrollIf === 'true' ){
var callbackBefore = function(element) {
if ( $attrs.callbackBefore ) {
var exprHandler = $scope.callbackBefore({element: element});
if (typeof exprHandler === 'function') {
exprHandler(element);
}
}
}
var callbackAfter = function(element) {
if ( $attrs.callbackAfter ) {
var exprHandler = $scope.callbackAfter({element: element});
if (typeof exprHandler === 'function') {
exprHandler(element);
}
}
}
smoothScroll($elem[0], {
duration: $attrs.duration,
offset: $attrs.offset,
easing: $attrs.easing,
callbackBefore: callbackBefore,
callbackAfter: callbackAfter
});
}
});
}
};
}])
// Scrolls to a specified element ID when this element is clicked
//
.directive('scrollTo', ['smoothScroll', function(smoothScroll){
return {
restrict: 'A',
scope: {
callbackBefore: '&',
callbackAfter: '&',
},
link: function($scope, $elem, $attrs){
var targetElement;
$elem.on('click', function(e){
targetElement = document.getElementById($attrs.scrollTo);
if ( targetElement ) {
e.preventDefault();
var callbackBefore = function(element) {
if ( $attrs.callbackBefore ) {
var exprHandler = $scope.callbackBefore({element: element});
if (typeof exprHandler === 'function') {
exprHandler(element);
}
}
}
var callbackAfter = function(element) {
if ( $attrs.callbackAfter ) {
var exprHandler = $scope.callbackAfter({element: element});
if (typeof exprHandler === 'function') {
exprHandler(element);
}
}
}
smoothScroll(targetElement, {
duration: $attrs.duration,
offset: $attrs.offset,
easing: $attrs.easing,
callbackBefore: callbackBefore,
callbackAfter: callbackAfter
});
return false;
}
});
}
};
}])
// Smooth scrolls the window to the provided element.
//
.factory('smoothScroll', ['$timeout', function($timeout){
var getScrollLocation = function() {
return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
};
var smoothScroll = function (element, options) {
$timeout(function(){
var startLocation = getScrollLocation(),
timeLapsed = 0,
percentage, position;
// Options
//
options = options || {};
var duration = options.duration || 800,
offset = options.offset || 0,
easing = options.easing || 'easeInOutQuart',
callbackBefore = options.callbackBefore || function() {},
callbackAfter = options.callbackAfter || function() {};
// 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 = getScrollLocation();
if ( position == endLocation || currentLocation == endLocation || ( (window.innerHeight + currentLocation) >= document.body.scrollHeight ) ) {
clearInterval(runAnimation);
callbackAfter(element);
}
};
// 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
//
callbackBefore(element);
var runAnimation = setInterval(animateScroll, 16);
});
};
return smoothScroll;
}]);