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>
This question is related to
jquery
scroll
jquery-animate
for simple Scroll, use following style
height: 200px; overflow: scroll;
and use this style class which div or section you want to show scroll
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();
});
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"});
Pure javascript solution with scrollIntoView() function:
document.getElementById('title1').scrollIntoView({block: 'start', behavior: 'smooth'});
_x000D_
<h2 id="title1">Some title</h2>
_x000D_
P.S. 'smooth' parameter now works from Chrome 61 as julien_c mentioned in the comments.
$(jQuery.browser.webkit ? "body": "html").animate({ scrollTop: $('#title1').offset().top }, 1000);
Source: Stackoverflow.com