Skip to content

Instantly share code, notes, and snippets.

@Kelderic
Last active January 12, 2017 14:44
Show Gist options
  • Save Kelderic/172c70fbd03c759d689c35bbaac09857 to your computer and use it in GitHub Desktop.
Save Kelderic/172c70fbd03c759d689c35bbaac09857 to your computer and use it in GitHub Desktop.
This is an element prototype that animates the element's horizontal scroll. By default, it scrolls the element all the way over to the right, and if an integer is passed in, it animates until the element's scrollLeft property equals that integer.
HTMLElement.prototype.scrollHorizontally = function( newScrollLeft ) {
var self = this;
newScrollLeft = newScrollLeft ? newScrollOffset : self.scrollWidth - self.offsetWidth
animate();
function animate(){
if ( self.scrollLeft < ( newScrollLeft ) ) {
self.scrollLeft = self.scrollLeft + 10;
window.requestAnimationFrame(animate);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment