scroll top in jquery code example
Example 1: javascript scroll to top of page
window.scrollTo(0, 0);
Example 2: scroll to element jquery
$('html, body').animate({
scrollTop: $("#grepperRocks").offset().top
});
Example 3: scroll to top jquery
window.scrollTo({top: 0, behavior: 'smooth'});
reference : https://stackoverflow.com/questions/15935318/smooth-scroll-to-top
Example 4: Scroll to the top of the page using JavaScript?
var topPage = document.getElementById(`top-page`)
topPage.onclick = () => window.scrollTo({ top: 0, behavior: 'smooth' })
window.onscroll = () => {
window.scrollY > 500
? (topPage.style.display = 'block')
: (topPage.style.display = 'none')
}
body {
background-color: #111;
height:5000px;
}
#top-page {
width: 50px;
position: fixed;
right: 30px;
bottom: 30px;
cursor: pointer;
color: white;
display: none;
}
<img id="top-page" src="https://svgshare.com/i/LW3.sv" alt="Top">
Example 5: scrool to top jquerry
$("a[href='#top']").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
Example 6: jquery get document scrolltop
var top = ($(window).scrollTop() || $("body").scrollTop());