Skip to content

Commit b4bbf57

Browse files
Merge pull request #154 from socialbase/dragScroll
thanks, raf wont work on ie9 but who cares. You can polyfill if needs be.
2 parents 2d2850f + 57dec58 commit b4bbf57

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

ngDraggable.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,38 @@ angular.module("ngDraggable", [])
509509
verticalScroll: attrs.verticalScroll || true,
510510
horizontalScroll: attrs.horizontalScroll || true,
511511
activationDistance: attrs.activationDistance || 75,
512-
scrollDistance: attrs.scrollDistance || 50,
513-
scrollInterval: attrs.scrollInterval || 250
512+
scrollDistance: attrs.scrollDistance || 15
514513
};
515514

515+
516+
var reqAnimFrame = (function() {
517+
return window.requestAnimationFrame ||
518+
window.webkitRequestAnimationFrame ||
519+
window.mozRequestAnimationFrame ||
520+
window.oRequestAnimationFrame ||
521+
window.msRequestAnimationFrame ||
522+
function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
523+
window.setTimeout(callback, 1000 / 60);
524+
};
525+
})();
526+
527+
var animationIsOn = false;
516528
var createInterval = function() {
517-
intervalPromise = $interval(function() {
529+
animationIsOn = true;
530+
531+
function nextFrame(callback) {
532+
var args = Array.prototype.slice.call(arguments);
533+
if(animationIsOn) {
534+
reqAnimFrame(function () {
535+
$rootScope.$apply(function () {
536+
callback.apply(null, args);
537+
nextFrame(callback);
538+
});
539+
})
540+
}
541+
}
542+
543+
nextFrame(function() {
518544
if (!lastMouseEvent) return;
519545

520546
var viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
@@ -547,10 +573,12 @@ angular.module("ngDraggable", [])
547573
}
548574
}
549575

576+
577+
550578
if (scrollX !== 0 || scrollY !== 0) {
551579
// Record the current scroll position.
552-
var currentScrollLeft = $document[0].documentElement.scrollLeft;
553-
var currentScrollTop = $document[0].documentElement.scrollTop;
580+
var currentScrollLeft = ($window.pageXOffset || $document[0].documentElement.scrollLeft);
581+
var currentScrollTop = ($window.pageYOffset || $document[0].documentElement.scrollTop);
554582

555583
// Remove the transformation from the element, scroll the window by the scroll distance
556584
// record how far we scrolled, then reapply the element transformation.
@@ -559,41 +587,36 @@ angular.module("ngDraggable", [])
559587

560588
$window.scrollBy(scrollX, scrollY);
561589

562-
var horizontalScrollAmount = $document[0].documentElement.scrollLeft - currentScrollLeft;
563-
var verticalScrollAmount = $document[0].documentElement.scrollTop - currentScrollTop;
590+
var horizontalScrollAmount = ($window.pageXOffset || $document[0].documentElement.scrollLeft) - currentScrollLeft;
591+
var verticalScrollAmount = ($window.pageYOffset || $document[0].documentElement.scrollTop) - currentScrollTop;
564592

565593
element.css('transform', elementTransform);
566594

567-
// On the next digest cycle, trigger a mousemove event equal to the amount we scrolled so
568-
// the element moves correctly.
569-
$timeout(function() {
570-
lastMouseEvent.pageX += horizontalScrollAmount;
571-
lastMouseEvent.pageY += verticalScrollAmount;
595+
lastMouseEvent.pageX += horizontalScrollAmount;
596+
lastMouseEvent.pageY += verticalScrollAmount;
572597

573-
$rootScope.$emit('draggable:_triggerHandlerMove', lastMouseEvent);
574-
});
598+
$rootScope.$emit('draggable:_triggerHandlerMove', lastMouseEvent);
575599
}
576600

577-
}, config.scrollInterval);
601+
});
578602
};
579603

580604
var clearInterval = function() {
581-
$interval.cancel(intervalPromise);
582-
intervalPromise = null;
605+
animationIsOn = false;
583606
};
584607

585608
scope.$on('draggable:start', function(event, obj) {
586609
// Ignore this event if it's not for this element.
587610
if (obj.element[0] !== element[0]) return;
588611

589-
if (!intervalPromise) createInterval();
612+
if (!animationIsOn) createInterval();
590613
});
591614

592615
scope.$on('draggable:end', function(event, obj) {
593616
// Ignore this event if it's not for this element.
594617
if (obj.element[0] !== element[0]) return;
595618

596-
if (intervalPromise) clearInterval();
619+
if (animationIsOn) clearInterval();
597620
});
598621

599622
scope.$on('draggable:move', function(event, obj) {

0 commit comments

Comments
 (0)