JavaScript Page Scroll Tutorial and Example

Today I wrote a simple JavaScript that scrolls down the page slowly for a certain amount of time, and then goes back up to the top of the page and starts scrolling again.


<html>
<head>
<script type="text/javascript">
scrolldelay = "";
function pageScroll() {
window.scrollBy(0,1); //Scroll 1px. Makes it smoother
scrolldelay = setTimeout('pageScroll()',50); //Scroll every 50ms
}
function startScroll() {
window.scroll(0,0);
clearTimeout(scrolldelay);
pageScroll();
scrolldelay = setTimeout('startScroll()',20000); //Repeat every 20 seconds
}
</script>
</head>
<body onLoad="startScroll()">
Put body text here
</body>
</html>

I used this code for a client who wanted a web page to be displayed publicly without needing to have a user do the work.

Let me know if you found this helpful!

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to JavaScript Page Scroll Tutorial and Example

  1. Xen says:

    Dude, i actually used this at work. ty 😀

  2. rocky123 says:

    I am trying to scroll the page to certain location but i need to add the delay to it how can i do that.
    i used
    window.scroll(0,100);
    to get to that point but need to add the delay. so that it stops at that point

Leave a Reply

Your email address will not be published. Required fields are marked *