Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automatically scroll down a html page?

I'm trying to start each page after the homepage about 500px down, similar to this website: http://unionstationdenver.com/

You'll notice when viewing pages after the homepage, you're automatically scrolled down without notice but you can than scroll up to revel the featured slider again.

I've played with scrolledHeight but I dont think that is what I need????

Basically I have a featured section that is on top of all my content pages, but you shouldn't be able to see this section until you scroll up. Any help?

like image 735
Hambone Avatar asked Jan 07 '12 23:01

Hambone


People also ask

How do I automatically scroll down a page in HTML?

The first one is with javascript: set the scrollTop property of the scrollable element (e.g. document. body. scrollTop = 1000; ). The second is setting the link to point to a specific id in the page e.g.

How do you auto scroll down a page?

To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.

How do I auto scroll to the bottom in HTML?

To auto scroll a page from top to bottom we can use scrollTop() and height() method in jquery. In this method pass the document's height in scrollTop method to scroll.


2 Answers

You can use .scrollIntoView() for this. It will bring a specific element into the viewport.

Example:

document.getElementById( 'bottom' ).scrollIntoView(); 

Demo: http://jsfiddle.net/ThinkingStiff/DG8yR/

Script:

function top() {     document.getElementById( 'top' ).scrollIntoView();     };  function bottom() {     document.getElementById( 'bottom' ).scrollIntoView();     window.setTimeout( function () { top(); }, 2000 ); };  bottom(); 

HTML:

<div id="top">top</div> <div id="bottom">bottom</div> 

CSS:

#top {     border: 1px solid black;     height: 3000px; }  #bottom {     border: 1px solid red; } 
like image 159
ThinkingStiff Avatar answered Oct 14 '22 08:10

ThinkingStiff


You can use two different techniques to achieve this.

The first one is with javascript: set the scrollTop property of the scrollable element (e.g. document.body.scrollTop = 1000;).

The second is setting the link to point to a specific id in the page e.g.

<a href="mypage.html#sectionOne">section one</a> 

Then if in your target page you'll have that ID the page will be scrolled automatically.

like image 25
Luca Avatar answered Oct 14 '22 07:10

Luca