Last active
August 29, 2015 14:10
-
-
Save forestrf/bcaf0a3c301dfabb257c to your computer and use it in GitHub Desktop.
scroll page to an element in the document or a top-pixel position
This file contains 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
// http://stackoverflow.com/questions/8917921/cross-browser-javascript-not-jquery-scroll-to-top-animation/23844067#23844067 | |
// e = element to scroll to or top position in pixels, d = time to scroll in ms | |
function scrollTo(e,d){if(d>=0){var h=document.documentElement;if(h.scrollTop===0){var t=h.scrollTop;++h.scrollTop;h=t+1===h.scrollTop--?h:document.body}typeof e==="object"&&(e=e.offsetTop);scrollToX(h,h.scrollTop,e,0,1/d,20)}} | |
/* | |
e = scrollable element | |
a = initiall scrollable element position (from) | |
b = height to scroll to (to) | |
t = lerp from a to b (0 - 1) | |
v = speed. input 1 / time to scroll | |
s = repeat every 's' miliseconds | |
*/ | |
function scrollToX(e,a,b,t,v,s){function F(){t<0||t>1||v<=0||(k=t-1,e.scrollTop=a-(a-b)*(k*k*k+1),t+=v*s,setTimeout(F,s))}F()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment