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.

Note

It is generally good practice to place your jQuery code/function inside the document load function so that the action takes place ONLY after the document has finished loading. This ensures that all of the page elements that you may be selecting are in place before running the code on them.


jQuery Notes:

  • To use jQuery on your site, it must first be downloaded from the official jQuery site and linked to in your document <head>, or linked to via a CDN in your document <head>
  • It is generally good practice to place your jQuery code/function inside the document load function so that the action takes place ONLY after the document has finished loading
  • When using jQuery, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent

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.