Skip to content

Instantly share code, notes, and snippets.

@inwardmovement
Last active Jan 27, 2020
Embed
What would you like to do?
Top button (using JS)
// TOP BUTTON
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 0 || document.documentElement.scrollTop > 0) {
document.getElementById("top-btn").style.display = "block";
} else {
document.getElementById("top-btn").style.display = "none";
}
}
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment