jQuery
HTML
CSS
JavaScript
PHP
SQL
WordPress
jQuery Snippets

Code Snippet

Some sites place a smooth-scrolling, back-to-top button in the lower, right-hand corner of the site so the user can get back to the page top easily.

This can be done using three things:

  • A block of HTML code to represent the back-to-top button
  • Some CSS to make it look nice
  • A block of jQuery code to make it smoothly scroll back to the top of the page

HTML

Place the following code just before the closing /body tag of your site.

				
					<!-- custom back to top button -->
    <a href="#" id="back-to-top" title="Back to top"><i class="fas fa-chevron-up"></i></a>
				
			

CSS

Then, in your CSS styles, place the following. This will give the button a nice look while adding to the functionality.

				
					/* custom back to top button */
#back-to-top {
    position: fixed;
    bottom: 40px;
    right: 24px;
    z-index: 9999;
    width: 54px;
    height: 54px;
    text-align: center;
    line-height: 48px;
    background: #225588;
    font-size: 25px;
    color: #ffffff;
    cursor: pointer;
    border: #ffffff solid thin;
    border-radius: 6px;
    text-decoration: none;
    transition: all 0.4s;
    opacity: 0;
}
#back-to-top:hover {
    background: #2c353d;
}
#back-to-top.show {
    opacity: 1;
}
				
			

jQuery

Finally, place the following in your JavaScript page. this gives the button its functionality.

  • Line 3 is the number of pixels the user must have scrolled down the page before the button will first appear
  • Line 20 is the speed in ms for how quickly the button will scroll back to the top of the page
				
					// custom back to top button
if ($('#back-to-top').length) {
    var scrollTrigger = 100, // px
        backToTop = function () {
            var scrollTop = $(window).scrollTop();
            if (scrollTop > scrollTrigger) {
                $('#back-to-top').addClass('show');
            } else {
                $('#back-to-top').removeClass('show');
            }
        };
    backToTop();
    $(window).on('scroll', function () {
        backToTop();
    });
    $('#back-to-top').on('click', function (e) {
        e.preventDefault();
        $('html,body').animate({
            scrollTop: 0
        }, 700);
    });
}
				
			

To see this in action, look to the bottom, right corner of this site for the working button.

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.

jQuery
HTML
CSS
JavaScript
PHP
SQL
WordPress

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

Feedback

If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.