Last active
August 29, 2015 13:59
-
-
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
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
'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