How to scroll the window using JQuery $.scrollTo() function
$(‘html, body’).animate({scrollTop: $(“#page”).offset().top}, 2000);
$(‘html, body’).animate({scrollTop: $(“#page”).offset().top}, 2000);
There is a workaround by using the history API on modern browsers with fallback on old ones: if(history.pushState) { history.pushState(null, null, ‘#myhash’); } else { location.hash=”#myhash”; } Credit goes to Lea Verou
If you want a nice slow animation scroll, for any anchor with href=”#bottom” this will scroll you to the bottom: $(“a[href=”#bottom”]”).click(function() { $(“html, body”).animate({ scrollTop: $(document).height() }, “slow”); return false; }); Feel free to change the selector.
jQuery Scroll Plugin since this is a question tagged with jquery i have to say, that this library has a very nice plugin for smooth scrolling, you can find it here: http://plugins.jquery.com/scrollTo/ Excerpts from Documentation: $(‘div.pane’).scrollTo(…);//all divs w/class pane or $.scrollTo(…);//the plugin will take care of this Custom jQuery function for scrolling you can use … Read more
You can calculate the offset of the element and then compare that with the scroll value like: $(window).scroll(function() { var hT = $(‘#scroll-to’).offset().top, hH = $(‘#scroll-to’).outerHeight(), wH = $(window).height(), wS = $(this).scrollTop(); if (wS > (hT+hH-wH)){ console.log(‘H1 on the view!’); } }); Check this Demo Fiddle Updated Demo Fiddle no alert — instead FadeIn() the … Read more
jQuery isn’t necessary. Most of the top results I got from a Google search gave me this answer: window.scrollTo(0, document.body.scrollHeight); Where you have nested elements, the document might not scroll. In this case, you need to target the element that scrolls and use it’s scroll height instead. window.scrollTo(0, document.querySelector(“.scrollingContainer”).scrollHeight); You can tie that to the … Read more