Answer to Question #340326 in HTML/JavaScript Web Application for Nathan

Question #340326

Create a link button at the bottom of the webpage that would make it jump to the top of the webpage when clicked

1
Expert's answer
2022-05-14T16:39:43-0400
<html>
<head>
    <button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
</body>
</html>



<script type="text/javascript">


mybutton = document.getElementById("myBtn");


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> 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS