Skip to content

Instantly share code, notes, and snippets.

@kkurni
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkurni/10682748 to your computer and use it in GitHub Desktop.
Save kkurni/10682748 to your computer and use it in GitHub Desktop.
scroll helper will recursively try to scroll into your element. this will be very useful in angularjs
'use strict';
commonLib.factory('scrollHelper', ['$timeout', function ($timeout) {
//scroll to element recursively until found
function scrollToElement(scrollToId, maxRetry) {
var counter = 0;
function tryScroll() {
counter ++;
var el = document.getElementById(scrollToId);
if (el) {
el.scrollIntoView(true);
} else if (counter < maxRetry) {
//try scroll again
$timeout(tryScroll, counter * 10);
}
};
$timeout(tryScroll, 0);
};
return {
scrollToElement: scrollToElement
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment