Jump to content

Jump to top button only displaying at the bottom of a page - how to make it always appear?

Recommended Posts

Hello, first off, I would like to state that I am using this code: 

<style>
	#myBtn {
       width: 50px;
       height: 50px;
       display: none;
       position: fixed;
       bottom: 20px;
       right: 30px;
       z-index: 1;
       border: none;
       outline: none;
       font-size: 25px !important;
       color: #17306e;
       cursor: pointer;
       padding: 10px 10px 10px 11px;
       border-radius: 50%;
       box-shadow: 1px 1px 5px #000;
       // Change the hex code in the next line to change background color
       background-color: #cbcccd;
    }

    #myBtn:hover {
       background-color: #aeaeae;
    }
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/js/all.min.js" crossorigin="anonymous"></script>
<button onclick="topFunction()" id="myBtn" title="Go to top">
    <i class="fas fa-arrow-up"></i>
</button>
<script>
   window.onscroll = function() {scrollFunction()};

   function scrollFunction() {
      if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
         document.getElementById("myBtn").style.display = "block";
      } else {
         document.getElementById("myBtn").style.display = "none";
      }
   }

   function topFunction() {
      $('html,body').animate({ scrollTop: 0 }, 1000);
   }
</script>

 

 

With that being said, the Jump to Top button only seems to appear once I've hit the bottom of a page. However, after I reach the bottom of the page, the button stays visible as I scroll back up. 

 

So my question is: what code do I need to add/modify to make the Jump to Top button stay visible as soon as you start to scroll down? I don't have a website to share, since my trial has expired. 

 

Thanks!

Edited by a_tree_gnome
Link to comment
  • Replies 1
  • Views 379
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.