问题
I have a website with one big horizontal div. It automatically scrolls vertically and horizontally onload (I used this javascript as discussed here: smooth auto scroll by using javascript):
function pageScroll() {
window.scrollBy(10,-10); // horizontal and vertical scroll increments
scrolldelay = setTimeout('pageScroll()',100); // scrolls every 100 milliseconds
}
What I would like to know but can't figure out: is it possible to toggle this function? I would like that once the page automatically scrolled to the end of the page, that it goes back up and back to the left again. So that it goes back and forth, up and down forever.
All help is very appreciated, thanks in advance!
回答1:
Working Fiddle http://jsfiddle.net/sajith/MJQ7u/
var xMax, yMax, xNeg=1, yNeg=1;
function pageScroll() {
window.scrollBy(10 * xNeg, 10 * yNeg);
if(xMax == window.scrollX)xNeg = xNeg * -1;
if(yMax == window.scrollY)yNeg = xNeg * -1;
scrolldelay = setTimeout(pageScroll,100);
console.log(window.scrollY);
xMax = window.scrollX;
yMax = window.scrollY;
}
来源:https://stackoverflow.com/questions/22188541/how-to-toggle-auto-scroll-function-reverse-page-scroll