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); });
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.
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.
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) ...
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.
You can do this using the .animate()
method:
$(document).ready(function () { // Handler for .ready() called. $('html, body').animate({ scrollTop: $('#what').offset().top }, 'slow'); });
what
FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With