Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions lib/angular-smooth-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
/**
* Smooth scrolls the window/div to the provided element.
*
* 20150713 EDIT - zephinzer
* Added new option - containerId to account for scrolling within a DIV
* 20150723 EDIT - TKuypers
* Added a option for horizontal scrolling
*/
var smoothScroll = function (element, options) {
options = options || {};
Expand All @@ -36,19 +36,20 @@
callbackBefore = options.callbackBefore || function() {},
callbackAfter = options.callbackAfter || function() {},
container = document.getElementById(options.containerId) || null,
containerPresent = (container != undefined && container != null);
containerPresent = (container != undefined && container != null),
dir = options.direction || 'vertical';

/**
* Retrieve current location
*/
var getScrollLocation = function() {
if(containerPresent) {
return container.scrollTop;
return (dir == 'vertical') ? container.scrollTop : container.scrollLeft;
} else {
if(window.pageYOffset) {
return window.pageYOffset;
if(window.pageYOffset || window.pageXOffset) {
return (dir == 'vertical') ? window.pageYOffset : window.pageXOffset;
} else {
return document.documentElement.scrollTop;
return (dir == 'vertical') ? document.documentElement.scrollTop : document.documentElement.scrollLeft;
}
}
};
Expand Down Expand Up @@ -85,7 +86,7 @@
var location = 0;
if (element.offsetParent) {
do {
location += element.offsetTop;
location += (dir == 'vertical') ? element.offsetTop : element.offsetLeft;
element = element.offsetParent;
} while (element);
}
Expand All @@ -102,19 +103,19 @@
distance = endLocation - startLocation,
percentage,
position,
scrollHeight,
internalHeight;
scrollSize,
internalSize;

/**
* Stop the scrolling animation when the anchor is reached (or at the top/bottom of the page)
*/
var stopAnimation = function () {
currentLocation = getScrollLocation();
internalHeight = window.innerHeight + currentLocation;
internalSize = ((dir == 'vertical') ? window.innerHeight : window.innerWidth) + currentLocation;
if(containerPresent) {
scrollHeight = container.scrollHeight;
scrollSize = (dir == 'vertical') ? container.scrollHeight : container.scrollWidth;
} else {
scrollHeight = document.body.scrollheight;
scrollSize = (dir == 'vertical') ? document.body.scrollheight : document.body.scrollWidth;
}

if (
Expand All @@ -125,7 +126,7 @@
currentLocation == endLocation
) ||
( // condition 3
internalHeight >= scrollHeight
internalSize >= scrollSize
)
) { // stop
clearInterval(runAnimation);
Expand All @@ -142,9 +143,17 @@
percentage = ( percentage > 1 ) ? 1 : percentage;
position = startLocation + ( distance * getEasingPattern(easing, percentage) );
if(containerPresent) {
container.scrollTop = position;
if(dir == 'vertical') {
container.scrollTop = position;
} else {
container.scrollLeft = position;
}
} else {
window.scrollTo( 0, position );
if(dir == 'vertical') {
window.scrollTo( 0, position );
} else {
window.scrollTo( position, 0);
}
}
stopAnimation();
};
Expand Down Expand Up @@ -256,6 +265,7 @@
duration: $attrs.duration,
offset: $attrs.offset,
easing: $attrs.easing,
direction: $attrs.direction,
callbackBefore: callbackBefore,
callbackAfter: callbackAfter,
containerId: $attrs.containerId
Expand All @@ -267,4 +277,4 @@
};
}]);

}());
}());