Fixed infinite loop on large displays
This commit is contained in:
parent
377d8ae7d3
commit
9f82dd214c
@ -4,6 +4,7 @@ var speed = .0005; //speed multiplier
|
|||||||
//setup necissary stuff
|
//setup necissary stuff
|
||||||
var target = null;
|
var target = null;
|
||||||
var targetHeight = null;
|
var targetHeight = null;
|
||||||
|
var prevHeight = null;
|
||||||
var currentHeight = null;
|
var currentHeight = null;
|
||||||
var startTime = null;
|
var startTime = null;
|
||||||
var count = 0;
|
var count = 0;
|
||||||
@ -12,6 +13,7 @@ function smoothScroll(targetId){
|
|||||||
target = document.getElementById(targetId);//grab the element
|
target = document.getElementById(targetId);//grab the element
|
||||||
targetHeight = target.offsetTop; //get its height
|
targetHeight = target.offsetTop; //get its height
|
||||||
currentHeight = getScrollY(); //grab the current scroll position
|
currentHeight = getScrollY(); //grab the current scroll position
|
||||||
|
prevHeight = null;
|
||||||
startTime = null; //we have to reset the start time every time the animation runs
|
startTime = null; //we have to reset the start time every time the animation runs
|
||||||
if(currentHeight != targetHeight){
|
if(currentHeight != targetHeight){
|
||||||
count = 0;
|
count = 0;
|
||||||
@ -22,12 +24,12 @@ function animateScroll(timeStamp){
|
|||||||
if (startTime == null) startTime = timeStamp; //record the time the animation started if we're just starting
|
if (startTime == null) startTime = timeStamp; //record the time the animation started if we're just starting
|
||||||
var deltaStep = (timeStamp - startTime) * speed; //timeStamp - startTime gives us miliseconds since the animation started, we scale this down because its usually in the hundreds.
|
var deltaStep = (timeStamp - startTime) * speed; //timeStamp - startTime gives us miliseconds since the animation started, we scale this down because its usually in the hundreds.
|
||||||
currentHeight = getScrollY(); //record the current scroll position of the window
|
currentHeight = getScrollY(); //record the current scroll position of the window
|
||||||
|
prevHeight = currentHeight;//store height before scroll action so we can compare it to the height after we try to scroll
|
||||||
var offsetScroll = targetHeight - currentHeight; //calculate how far we still need to go
|
var offsetScroll = targetHeight - currentHeight; //calculate how far we still need to go
|
||||||
var deltaScroll = deltaStep * offsetScroll; //decide how far to scroll this tick
|
var deltaScroll = deltaStep * offsetScroll; //decide how far to scroll this tick
|
||||||
window.scroll(0, currentHeight + deltaScroll); //scroll!
|
window.scroll(0, currentHeight + deltaScroll); //scroll!
|
||||||
currentHeight = getScrollY();//re-record the current height after scrolling for comparison
|
currentHeight = getScrollY();//re-record the current height after scrolling for comparison
|
||||||
count++;
|
if((currentHeight != targetHeight) && (currentHeight != prevHeight)){ //schedule another tick until we get where we're going or we get as far as we can
|
||||||
if((currentHeight != targetHeight) || ((currentHeight + offsetScroll < window.innerHeight)&& count < 50)){ //schedule another tick until we get where we're going or we get as far as we can
|
|
||||||
window.requestAnimationFrame(animateScroll);
|
window.requestAnimationFrame(animateScroll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user