javascript scroll up code example
Example 1: javascript go to top of page
window.scroll({
top: 0,
left: 0,
behavior: 'smooth'
});
Example 2: 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:
height:5000px;
}
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 3: scrool to top jquerry
$("a[href='#top']").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
Example 4: scroll down up js
var lastScrollTop = 0;
element.addEventListener("scroll", function(){
var st = window.pageYOffset || document.documentElement.scrollTop;
if (st > lastScrollTop){
} else {
}
lastScrollTop = st <= 0 ? 0 : st;
}, false);
Example 5: javascript scroll down
window.scrollTo(300, 500);