Scroll-to-top-button

Adding a scroll to top button to your website can be a great way to allow your users to navigate, especially if you have long-scrolling pages. A scroll button should be subtle and it has become expected to see one on the lower right of the page. Some sites include the button in the footer while others use fixed positioning to place the button and show/hide the button based on where the user is on the page. This tutorial will cover how to display the scroll button using jQuery when the user scrolls down the page a little. We’ll use an icon font from Font Awesome but you could easily substitute a Bootstrap glyph or any other icon.

 
 

Style and Script Libraries

We need to make sure two libraries are present on our page. The first is jQuery, as we’ll be using that to hide/show the button and to scroll the page when the user clicks the button.

If jQuery is not already in use on your site, add the following just before the closing <body> tag of the page.

The next step is to add the icon font library. If you’re not already using Font Awesome, then include the following to the <head> element of your page.

 
 

The HTML Markup

The HTML for the button is pretty straight forward. It may change slightly if you use an icon font, an image, or text. We’ll be using an icon font for this demonstration. Add the anywhere in the <body> element of the page.

 
 

CSS Styles

The CSS for the button is pretty straight forward as well. The colors, sizes, and other properties can be changed, but the important styles are the position, visibility, and opacity properties. Add the following to the <head> element.

The .show class is used to change the visibility of the button, the default style keeps the button hidden from view. There are also transition properties to fade the button in and out when shown. This transition is why we aren’t using display:none; or jQuery.hide() to hide the button.

 
 

The Javascript

There are two components that need to be handled by jQuery. The first is to show/hide the button as the user scrolls around the page. The second is to scroll to the top of the page when the user clicks the button. To begin create an empty <script> element right after the jQuery library script at the bottom of the <body> element.

 

Show and Hide the Button

To show and hide the button we use the jQuery ‘scroll’ event to detect if the user is scrolling. Check the top of the window and detect the offset to the top of the page, if it’s greater then 100 pixels show the button by adding the ‘show’ class to the ‘scroll-top-wrapper’ element. That 100 pixel offset is arbitrary and can be changed to suit your site.

 

Scroll to Top

The next step is to handle a button click and scroll to the top of the page. To do this we use the jQuery ‘click’ event. The scrollToTop function uses the jQuery animate method to scroll up with animation rather than instantly.

 

Thant’s all there is to it, you should be able to load your page and see a button that looks something like this appear.

Scroll-to-top-button

 
 

Update on 3/30/14

If you’re using WordPress you can now download my WordPress plugin which is built on these techniques.

One comment on “Adding a Scroll to Top Button to a Website

  1. Thanks for the tutorial. Only the .js and html isn’t correct (or the css). The class ‘scroll-back-to-top-wrapper’ in the css isn’t used in the .js or html. There ‘scroll-top-wrapper’ is used. Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">