Animate scroll to ID on page load

JqueryScrollJquery Animate

Jquery Problem Overview


Im tring to animate the scroll to a particular ID on page load. I have done lots of research and came across this:

$("html, body").animate({ scrollTop: $('#title1').height() }, 1000);

but this seems to start from the ID and animate to the top of the page?

The HTML (which is half way down the page) is simply:

<h2 id="title1">Title here</h2>

Jquery Solutions


Solution 1 - Jquery

You are only scrolling the height of your element. offset() returns the coordinates of an element relative to the document, and top param will give you the element's distance in pixels along the y-axis:

$("html, body").animate({ scrollTop: $('#title1').offset().top }, 1000);

And you can also add a delay to it:

$("html, body").delay(2000).animate({scrollTop: $('#title1').offset().top }, 2000);

Solution 2 - Jquery

Pure javascript solution with scrollIntoView() function:

document.getElementById('title1').scrollIntoView({block: 'start', behavior: 'smooth'});

<h2 id="title1">Some title</h2>

P.S. 'smooth' parameter now works from Chrome 61 as julien_c mentioned in the comments.

Solution 3 - Jquery

Deprecation Notice: The jQuery.browser property was removed in jQuery 1.9. Visit the docs for more details: https://api.jquery.com/jQuery.browser/

$(jQuery.browser.webkit ? "body": "html").animate({ scrollTop: $('#title1').offset().top }, 1000);

Source: jQuery Animate Body Scroll For All Browsers

Solution 4 - Jquery

There is a jquery plugin for this. It scrolls document to a specific element, so that it would be perfectly in the middle of viewport. It also supports animation easings so that the scroll effect would look super smooth. Check this link.

In your case the code is

$("#title1").animatedScroll({easing: "easeOutExpo"});

Solution 5 - Jquery

try with following code. make elements with class name page-scroll and keep id name to href of corresponding links

$('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: ($($anchor.attr('href')).offset().top - 50)
        }, 1250, 'easeInOutExpo');
        event.preventDefault();
    });

Solution 6 - Jquery

for simple Scroll, use following style

height: 200px; overflow: scroll;

and use this style class which div or section you want to show scroll

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdiView Question on Stackoverflow
Solution 1 - JqueryBumbleB2naView Answer on Stackoverflow
Solution 2 - JqueryVladimir VovkView Answer on Stackoverflow
Solution 3 - JqueryAleX gReyView Answer on Stackoverflow
Solution 4 - JqueryEugene TiurinView Answer on Stackoverflow
Solution 5 - JqueryAkhilView Answer on Stackoverflow
Solution 6 - Jquerym.m View Answer on Stackoverflow