Back To Top

Back To Top

This "Back to top" link allows users to smoothly scroll back to the top of the page. It's a little detail which enhances navigation experience on website with long pages.

Nucleo icons

Sponsored by Nucleo, a free application to collect, customize and export all your icons as icon font and SVG symbols. Made by the CodyHouse folks!

Do you want to include this resource in a product offered for sale? Learn more about our Extended License

This resource is suitable for website with lots of page content. The link fades in on the right-hand side of the content area, after the browser window has been scrolled beyond a certain point, and remains fixed during scrolling.
If users keeps on scrolling, the button nicely reduces its opacity to be less distracting during navigation.

Creating the structure

We inserted the "Back to top" link at the bottom of our content, before the closing tag.

 <!-- all your content here -->
 <a href="#0" class="cd-top js-cd-top">Top</a>

Adding style

The link will appear fixed on the right-side of the content. Initially it has a visibility:hidden; and an opacity:0;.
Both visibility and opacity are controlled through two classes: .cd-top--show and .cd-top--fade-out. { 
 /* the button becomes visible */ 
 visibility: visible;
 opacity: 1; { 
 /* if the user keeps scrolling down, the button is out of focus and becomes less visible */ 
 opacity: .5; 

These classes are added (or removed) to the "Back to top" link using JavaScript.

Events handling

In our .js file, we have defined three variables to control the "Back to top" link appearance:

//browser window scroll (in pixels) after which the "back to top" link is shown
var offset = 300, 
    //browser window scroll (in pixels) after which the "back to top" link opacity is reduced 
    offsetOpacity = 1200,
    //duration of the top scrolling animation (in ms)
    scrollDuration = 700;

The offset variable will be used to toggle the class .cd-top--show; the offsetOpacity, instead, to add the .cd-top--fade-out class. The top scrolling function is bound to the "Back to top" click event.

Join our newsletter

Get our monthly recap with the latest CodyHouse news