Back to top - New feature

Post

Posted
Rating:
Item has a rating of 5 (Liked by Chris GrahamLiked by Joey)
#2890 (In Topic #521)
Avatar
Standard member
Paul Flavel is in the usergroup ‘Honoured member’
Here is a quick tutorial on how to create a conditional back to top button as you scroll down the page.

Open your GLOBAL_HTML_WRAP.tpl

Just before </body> add the following.

{+START,IF,{$NOT,{$MOBILE}}}
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<script>
// When the user scrolls down 20px from the top of the document, show the button
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";
    }
}

// When the user clicks on the button, scroll to the top of the document
function topFunction() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
}
</script>
{+END}



Then in your global.css add the following down the bottom:

Code

#myBtn {
  display: none;
  position: fixed;
  bottom: 20px;
  right: 30px;
  z-index: 99;
  border: none;
  outline: none;
  background-color: red;
  color: white;
  cursor: pointer;
  padding: 15px;
  border-radius: 10px;
}

#myBtn:hover {
  background-color: #555;
}


Customise the css to suite your site


Enjoy!
Online now: No Back to the top
1 guest and 0 members have just viewed this.

Statistics

Users online:

babu, Salman, gabriel58, Vaiva

Forum statistics:
  • 1,101 topics, 5,301 posts, 6,187 members
  • Our newest member is miracleworx78
Birthdays:
Back to Top