1<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
2
3
4<script>
5//Get the button
6var mybutton = document.getElementById("myBtn");
7
8// When the user scrolls down 20px from the top of the document, show the button
9window.onscroll = function() {scrollFunction()};
10
11function scrollFunction() {
12 if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
13 mybutton.style.display = "block";
14 } else {
15 mybutton.style.display = "none";
16 }
17}
18
19// When the user clicks on the button, scroll to the top of the document
20function topFunction() {
21 document.body.scrollTop = 0;
22 document.documentElement.scrollTop = 0;
23}
24</script>
25