Skip to content

Commit

Permalink
Merge pull request #48 from HHogg/cancel-on-scroll
Browse files Browse the repository at this point in the history
Issue #40: Cancel scroll on mousewheel
  • Loading branch information
oblador committed Jul 7, 2014
2 parents 239fc57 + f5c7b4e commit 68a9ab4
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ run(function($window, $q, cancelAnimation, requestAnimation, duScrollEasing) {
this.$get = function() {
return proto;
};

var isDocument = function(el) {
return (typeof HTMLDocument !== 'undefined' && el instanceof HTMLDocument) || (el.nodeType && el.nodeType === el.DOCUMENT_NODE);
};
Expand Down Expand Up @@ -45,7 +45,6 @@ run(function($window, $q, cancelAnimation, requestAnimation, duScrollEasing) {
deltaLeft = Math.round(left - startLeft),
deltaTop = Math.round(top - startTop);


var startTime = null;
if(scrollAnimation) {
cancelAnimation(scrollAnimation);
Expand All @@ -59,28 +58,35 @@ run(function($window, $q, cancelAnimation, requestAnimation, duScrollEasing) {
return deferred.promise;
}

var cancelOnScroll = function() {
cancelAnimation(scrollAnimation);
deferred.reject();
};

var animationStep = function(timestamp) {
if (startTime === null) {
startTime = timestamp;
}

var progress = timestamp - startTime;
var percent = (progress >= duration ? 1 : easing(progress/duration));

el.scrollTo(
startLeft + Math.ceil(deltaLeft * percent),
startTop + Math.ceil(deltaTop * percent)
);
if(percent < 1) {
scrollAnimation = requestAnimation(animationStep);
} else {
angular.element($window).unbind('mousewheel', cancelOnScroll);
scrollAnimation = null;
deferred.resolve();
}
};

//Fix random mobile safari bug when scrolling to top by hitting status bar
el.scrollTo(startLeft, startTop);
angular.element($window).bind('mousewheel', cancelOnScroll);

scrollAnimation = requestAnimation(animationStep);
return deferred.promise;
Expand All @@ -105,7 +111,7 @@ run(function($window, $q, cancelAnimation, requestAnimation, duScrollEasing) {
return $window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft;
}
return el.scrollLeft;
},
},
scrollTop: function(value, duration, easing) {
if(angular.isNumber(value)) {
return this.scrollTo(this.scrollTop(), value, duration, easing);
Expand All @@ -118,7 +124,7 @@ run(function($window, $q, cancelAnimation, requestAnimation, duScrollEasing) {
}
};

//Add duration and easing functionality to existing jQuery getter/setters
//Add duration and easing functionality to existing jQuery getter/setters
var overloadScrollPos = function(superFn, overloadFn) {
return function(value, duration, easing) {
if(duration) {
Expand Down

0 comments on commit 68a9ab4

Please sign in to comment.