How to toggle auto scroll function / reverse page scroll?

送分小仙女□ 提交于 2019-12-24 05:47:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!