Skip to content

Instantly share code, notes, and snippets.

@blazsmaster
Created August 30, 2021 11:33
Show Gist options
  • Save blazsmaster/c2a4c28a34dc3a53823fde290d50cb7e to your computer and use it in GitHub Desktop.
Save blazsmaster/c2a4c28a34dc3a53823fde290d50cb7e to your computer and use it in GitHub Desktop.
Simple scroll up script for html websites.
<button type="button" id="topButton" onclick="topFunction()">Top</button>
<style>
#topButton {
display: none;
position: fixed;
bottom: 25px;
right: 30px;
z-index: 99;
}
</style>
<script>
var mybutton = document.getElementById('topButton');
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = 'block';
} else {
mybutton.style.display = 'none';
};
};
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment