Autoscroll page with jQuery

David Carr

Javascript Tutorials

There are times when it’s really useful for a webpage to scroll up and down automatically. For instance when showing information on a TV screen you may not want to scroll manually. Using jQuery its possible to have the page automatically scroll down at a set speed then scroll back to the top and repeat.

Demo

Download source files

jQuery must be included before the follow code, this can be placed in a document.ready call or placed in an js file I’m using a file called app.js I have a bunch of placeholder text to beef up the page content so there is something to scroll against:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Auto Scroll with jQuery</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>

<h1>Auto Scroll with jQuery</h1>

<p>
    Bob called an all-hands this afternoon. Golden goose cloud strategy. Shelfware. Can I just chime in on that one get buy-in nor social currency overcome key issues to meet key milestones critical mass, but helicopter view. What's the status on the deliverables for eow? show pony, and bench mark. Not enough bandwidth back of the net. Re-inventing the wheel granularity, for not enough bandwidth, where do we stand on the latest client ask overcome key issues to meet key milestones waste of resources. Game plan get buy-in. Idea shower. Work you better eat a reality sandwich before you walk back in that boardroom cross-pollination herding cats hit the ground running pixel pushing, and turd polishing. After I ran into Helen at a restaurant, I realized she was just office pretty hit the ground running or what's the status on the deliverables for eow?, this proposal is a win-win situation which will cause a stellar paradigm shift, and produce a multi-fold increase in deliverables. Execute show pony message the initiative dog and pony show. Due diligence we need distributors to evangelize the new line to local markets fire up your browser, so cloud strategy obviously. Get six alpha pups in here for a focus group three-martini lunch. Market-facing best practices, yet put a record on and see who dances powerPointless, nor back of the net, for helicopter view. Come up with something buzzworthy pull in ten extra bodies to help roll the tortoise, and are there any leftovers in the kitchen? run it up the flagpole, ping the boss and circle back, nor action item. This is a no-brainer. Take five, punch the tree, and come back in here with a clear head critical mass, and core competencies, yet those options are already baked in with this model, cross sabers globalize new economy. Nail jelly to the hothouse wall deliverables, good optics but quick win. Not enough bandwidth.
</p>

<p>
    Bob called an all-hands this afternoon. Golden goose cloud strategy. Shelfware. Can I just chime in on that one get buy-in nor social currency overcome key issues to meet key milestones critical mass, but helicopter view. What's the status on the deliverables for eow? show pony, and bench mark. Not enough bandwidth back of the net. Re-inventing the wheel granularity, for not enough bandwidth, where do we stand on the latest client ask overcome key issues to meet key milestones waste of resources. Game plan get buy-in. Idea shower. Work you better eat a reality sandwich before you walk back in that boardroom cross-pollination herding cats hit the ground running pixel pushing, and turd polishing. After I ran into Helen at a restaurant, I realized she was just office pretty hit the ground running or what's the status on the deliverables for eow?, this proposal is a win-win situation which will cause a stellar paradigm shift, and produce a multi-fold increase in deliverables. Execute show pony message the initiative dog and pony show. Due diligence we need distributors to evangelize the new line to local markets fire up your browser, so cloud strategy obviously. Get six alpha pups in here for a focus group three-martini lunch. Market-facing best practices, yet put a record on and see who dances powerPointless, nor back of the net, for helicopter view. Come up with something buzzworthy pull in ten extra bodies to help roll the tortoise, and are there any leftovers in the kitchen? run it up the flagpole, ping the boss and circle back, nor action item. This is a no-brainer. Take five, punch the tree, and come back in here with a clear head critical mass, and core competencies, yet those options are already baked in with this model, cross sabers globalize new economy. Nail jelly to the hothouse wall deliverables, good optics but quick win. Not enough bandwidth.
</p>

<p>
    Bob called an all-hands this afternoon. Golden goose cloud strategy. Shelfware. Can I just chime in on that one get buy-in nor social currency overcome key issues to meet key milestones critical mass, but helicopter view. What's the status on the deliverables for eow? show pony, and bench mark. Not enough bandwidth back of the net. Re-inventing the wheel granularity, for not enough bandwidth, where do we stand on the latest client ask overcome key issues to meet key milestones waste of resources. Game plan get buy-in. Idea shower. Work you better eat a reality sandwich before you walk back in that boardroom cross-pollination herding cats hit the ground running pixel pushing, and turd polishing. After I ran into Helen at a restaurant, I realized she was just office pretty hit the ground running or what's the status on the deliverables for eow?, this proposal is a win-win situation which will cause a stellar paradigm shift, and produce a multi-fold increase in deliverables. Execute show pony message the initiative dog and pony show. Due diligence we need distributors to evangelize the new line to local markets fire up your browser, so cloud strategy obviously. Get six alpha pups in here for a focus group three-martini lunch. Market-facing best practices, yet put a record on and see who dances powerPointless, nor back of the net, for helicopter view. Come up with something buzzworthy pull in ten extra bodies to help roll the tortoise, and are there any leftovers in the kitchen? run it up the flagpole, ping the boss and circle back, nor action item. This is a no-brainer. Take five, punch the tree, and come back in here with a clear head critical mass, and core competencies, yet those options are already baked in with this model, cross sabers globalize new economy. Nail jelly to the hothouse wall deliverables, good optics but quick win. Not enough bandwidth.
</p>


<script data-cfasync="false" src="jquery.js"></script>
<script data-cfasync="false" src="app.js"></script>
</body>
</html>

The important code is inside app.js:

//scroll to bottom
setInterval(function(){

    //time to scroll to bottom
    $("html, body").animate({ scrollTop: $(document).height() }, 1000);

    //scroll to top
    setTimeout(function() {
       $('html, body').animate({scrollTop:0}, 8000);
    },2000);//call every 2000 miliseconds

},2000);//call every 2000 miliseconds

setInterval accepts 2 params the first is a function, the second is a time to repeat. Inside the function a call to animate is made which will animate the body of the page and scroll th the bottom of the document as a speed of 1000 microseconds adjust this to suit your page contents.

Next a setTimeout exists to animate back to the top of the page at a speed of 8000 microseconds.

Read articles directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Copyright © 2006 - 2024 DC Blog - All rights reserved.