Created
October 10, 2013 04:42
-
-
Save reu/22988a9bb3cb12fba9ed to your computer and use it in GitHub Desktop.
Angular.js autoscroll directive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module("autoscroll", []) | |
.service("scroller", function() { | |
this.defaultTime = 500; | |
this.scroll = function(element, height, time) { | |
element.animate({ | |
scrollTop: height | |
}, time || this.defaultTime); | |
} | |
this.scrollUp = function(element, time) { | |
this.scroll(element, 0, time); | |
} | |
this.scrollDown = function(element, time) { | |
this.scroll(element, element.prop("scrollHeight"), time); | |
} | |
}) | |
.directive("autoScroll", ["$timeout", "scroller", function($timeout, scroller) { | |
return function(scope, element, attrs) { | |
var direction = attrs.autoScroll; | |
scope.$watch(function() { | |
return element.children().length; | |
}, function() { | |
switch(direction) { | |
case "up": | |
scroller.scrollUp(element, 500); | |
break; | |
case "down": | |
scroller.scrollDown(element, 500); | |
break; | |
default: | |
throw "You must specify an 'up' or 'down' autoscroll direction."; | |
} | |
}); | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment