Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to specific div on page load

I'm trying to figure out how get the page automaticlly scroll to a specific div when the page has loaded. I have tried using the jQuery scroll function, but cant get it to work correctly. Any suggestions?

Following is what i have tried so far:

jQuery(function() { jQuery(window).scrollTop(jQuery('.container').offset().top); }); 
like image 831
simon Avatar asked Aug 07 '13 12:08

simon


People also ask

How do I scroll to a certain div on page load?

Just get the div id from query string and on page load pass it to above code like: $('#' + divToScrollTo). offset(). top , where divToScrollTo is js variable where we have stored the query string value.

How do I scroll to a specific div in HTML?

If you want to scroll the current document to a particular place, the value of HREF should be the name of the anchor to which to scroll, preceded by the # sign. If you want to open another document at an anchor, give the URL for the document, followed by #, followed by the name of the anchor.

How do you scroll to the bottom of the div on load?

Use scrollTop and scrollHeight to Scroll to the Bottom of Div in JavaScript. A combination of scrollTop and scrollHeight can cause an element to scroll to the bottom because scrollTop determines the number of pixels for a vertical scroll. In contrast, scrollHeight is the element's height (visible and non-visible parts) ...

How do you scroll automatically to the bottom of the div using jQuery?

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.


1 Answers

You can do this using the .animate() method:

$(document).ready(function () {     // Handler for .ready() called.     $('html, body').animate({         scrollTop: $('#what').offset().top     }, 'slow'); }); 
  • This will smooth scroll to the div with ID what

FIDDLE

like image 180
palaѕн Avatar answered Sep 18 '22 12:09

palaѕн