Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cancelScrollAnimation call when user manually scrolls or when window is already at the bottom of the document #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 27 additions & 13 deletions angular-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var duScroll = angular.module('duScroll', [
//Default easing function for scroll animation
.value('duScrollEasing', duScrollDefaultEasing)
//Which events on the container (such as body) should cancel scroll animations
.value('duScrollCancelOnEvents', 'scroll mousedown mousewheel touchmove keydown')
.value('duScrollCancelOnEvents', 'scroll mousedown wheel DOMMouseScroll mousewheel touchmove keydown')
//Whether or not to activate the last scrollspy, when page/container bottom is reached
.value('duScrollBottomSpy', false)
//Active class name
Expand Down Expand Up @@ -91,8 +91,8 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
var startTime = null, progress = 0;
var el = this;

var cancelScrollAnimation = function($event) {
if (!$event || (progress && $event.which > 0)) {
var cancelScrollAnimation = function($event){
if(!$event || (progress && ($event.which > 0 || $event.type === 'wheel' || $event.type === 'mousedown' || $event.type === 'mousewheel'))){
if(duScrollCancelOnEvents) {
el.unbind(duScrollCancelOnEvents, cancelScrollAnimation);
}
Expand All @@ -107,7 +107,18 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
}
deferred = $q.defer();

if(duration === 0 || (!deltaLeft && !deltaTop)) {
var elem = unwrap(el);
var isScrollBottom = false;
var scrollY;
if(isDocument(elem)){
scrollY = $window.scrollY || elem.documentElement.scrollTop || elem.body.scrollTop;
isScrollBottom = elem.documentElement.scrollHeight - scrollY === elem.documentElement.clientHeight;
}
else if(isElement(elem)){
isScrollBottom = elem.scrollHeight - elem.scrollTop === elem.clientHeight;
}

if(duration === 0 || (!deltaLeft && (!deltaTop || (deltaTop > 0 && isScrollBottom) ))){
if(duration === 0) {
el.duScrollTo(left, top);
}
Expand Down Expand Up @@ -361,15 +372,18 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
};

var destroyContext = function($scope) {
var id = $scope.$id;
var context = contexts[id], container = context.container;
if(context.intervalPromise) {
$interval.cancel(context.intervalPromise);
}
if(container) {
container.off('scroll', context.handler);
}
delete contexts[id];
var id = $scope.$id;
var context = contexts[id],
container = context && context.container,
intervalPromise = context && context.intervalPromise;

if(intervalPromise){
$interval.cancel(context.intervalPromise);
}
if(container){
container.off('scroll', context.handler);
}
delete contexts[id];
};

var defaultContextId = createContext($rootScope);
Expand Down
2 changes: 1 addition & 1 deletion angular-scroll.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion angular-scroll.min.js.map

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
var startTime = null, progress = 0;
var el = this;

var cancelScrollAnimation = function($event) {
if (!$event || (progress && $event.which > 0)) {
var cancelScrollAnimation = function($event){
if(!$event || (progress && ($event.which > 0 || $event.type === 'wheel' || $event.type === 'mousedown' || $event.type === 'mousewheel'))){
if(duScrollCancelOnEvents) {
el.unbind(duScrollCancelOnEvents, cancelScrollAnimation);
}
Expand All @@ -63,7 +63,18 @@ angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
}
deferred = $q.defer();

if(duration === 0 || (!deltaLeft && !deltaTop)) {
var elem = unwrap(el);
var isScrollBottom = false;
var scrollY;
if(isDocument(elem)){
scrollY = $window.scrollY || elem.documentElement.scrollTop || elem.body.scrollTop;
isScrollBottom = elem.documentElement.scrollHeight - scrollY === elem.documentElement.clientHeight;
}
else if(isElement(elem)){
isScrollBottom = elem.scrollHeight - elem.scrollTop === elem.clientHeight;
}

if(duration === 0 || (!deltaLeft && (!deltaTop || (deltaTop > 0 && isScrollBottom) ))){
if(duration === 0) {
el.duScrollTo(left, top);
}
Expand Down
2 changes: 1 addition & 1 deletion src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var duScroll = angular.module('duScroll', [
//Default easing function for scroll animation
.value('duScrollEasing', duScrollDefaultEasing)
//Which events on the container (such as body) should cancel scroll animations
.value('duScrollCancelOnEvents', 'scroll mousedown mousewheel touchmove keydown')
.value('duScrollCancelOnEvents', 'scroll mousedown wheel DOMMouseScroll mousewheel touchmove keydown')
//Whether or not to activate the last scrollspy, when page/container bottom is reached
.value('duScrollBottomSpy', false)
//Active class name
Expand Down
21 changes: 12 additions & 9 deletions src/services/spy-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,18 @@ angular.module('duScroll.spyAPI', ['duScroll.scrollContainerAPI'])
};

var destroyContext = function($scope) {
var id = $scope.$id;
var context = contexts[id], container = context.container;
if(context.intervalPromise) {
$interval.cancel(context.intervalPromise);
}
if(container) {
container.off('scroll', context.handler);
}
delete contexts[id];
var id = $scope.$id;
var context = contexts[id],
container = context && context.container,
intervalPromise = context && context.intervalPromise;

if(intervalPromise){
$interval.cancel(context.intervalPromise);
}
if(container){
container.off('scroll', context.handler);
}
delete contexts[id];
};

var defaultContextId = createContext($rootScope);
Expand Down
4 changes: 3 additions & 1 deletion test/unit/helpersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ describe('jqlite helpers', function() {
});
}));

it('should cancel previous animation', function(done){inject(function($document, $rootScope) {
it('should cancel previous animation', function(done){inject(function($document, $rootScope, $window) {
var rejected = false;
$window.scrollY = 400;

$document.scrollTopAnimated(200, duration)
.catch(function() {
rejected = true;
Expand Down