Create a Hovering Scroll to Top Button With JQuery

JQuery Hovering Scroll to Top Button

JQuery Hovering Scroll to Top Button

Looking for an easy way to help your readers power through long web pages? Sick of scrolling for minutes at a time to get to the top of the document? Here’s a simple Scroll to Top button that hovers in the bottom right corner of your screen. Best of all, it’s made entirely from JQuery, CSS, and HTML, so you can easily drop it into preexisting pages and customize it to your hearts content.

The code is largely adapted from Brian Cray and David Walsh, but it’s a combination of the two ideas with the addition of a fade effect on the button and smooth scrolling action. Also, I moved the button to the bottom right corner of the page because I thought it was a little less intrusive. The browser will nicely and smoothly scroll up to the anchor when users click the link, and this is thanks to the super helpful ScrollTo plugin for JQuery, built by Ariel Flesler.

What’s that? You want to see it in action? Okay, you should check out the demo!

And without further ado, here’s the code. First, add the button and the anchor to your web page or template. The button should go at the bottom of your page, right above the </body> tag. And, the anchor should go at the top of your page.

<div id="top"></div> <!--this goes at the top-->
<div id="message"><a href="#top" id="top-link">Scroll to Top</a></div> <!--this goes at the bottom-->

Now, you’ll need some CSS to format the look and feel of the button. As you probably noticed, I left a gap at the bottom of the page so you can read the last few lines of text without being obstructed by the button. Here’s the stylesheet:

.container {padding: 0 0 70px 0;} /* the gap for the bottom */

#message
{
/* display: block before hiding */
display: block;
display: none;

/* link is above all other elements */
z-index: 999;

/* link doesn't hide text behind it */
opacity: .8;

/* link stays at same place on page */
position: fixed;

/* link goes at the bottom of the page */
top: 100%;
margin-top: -80px; /* = height + preferred bottom margin */

/* link is centered */
left: 80%;
margin-left: -80px;

/* round the corners (to your preference) */
-moz-border-radius: 24px;
-webkit-border-radius: 24px;

/* make it big and easy to see (size, style to preferences) */
width: 300px;
line-height: 48px;
height: 48px;
padding: 10px;
background-color: #000;
font-size: 24px;
text-align: center;
}

#message a { color: #fff; }

And, of course, the JQuery. Make sure you have JQuery and the ScrollTo plugin included in the source:

$(function () { // run this code on page load (AKA DOM load)

/* set variables locally for increased performance */
var scroll_timer;
var displayed = false;
var $message = $('#message');
var $window = $(window);
var top = $(document.body).children(0).position().top;

/* react to scroll event on window */
$window.scroll(function () {
window.clearTimeout(scroll_timer);
scroll_timer = window.setTimeout(function () { // use a timer for performance
if($window.scrollTop() <= top) // hide if at the top of the page
{
displayed = false;
$message.fadeOut(500);
}
else if(displayed == false) // show if scrolling down
{
displayed = true;
$message.stop(true, true).fadeIn(500).click(function () { $message.fadeOut(500); });
}
}, 100);
});
$('#top-link').click(function(e) {
e.preventDefault();
$.scrollTo(0,300);
});
});

… and there you have it! Let me know how it works for you in the comments, and feel free to share the code snippet with friends. Include your suggestions as well: I’m interested in good ways to expand the functionality.

@simplrdesgn: No public Twitter messages.
Want more? Follow me on Twitter!

One Comment

  1. Cañolimón
    Posted November 9, 2009 at 11:02 am | Permalink

    There’s a problem: When scrolling all the way down if yo click on the dark box and not the link, the box dissapears and the page doesn’t scroll back up.

    To have the box appear you have to ‘manually’ scroll back up and then down again.

    This is a CSS issue, if you style the link itself like the entire box and not the outside div (), that should solve this problem.

One Trackback

  1. By dot Blog. The week in links 09/11/09 on November 9, 2009 at 6:05 am

    [...] Create A Hovering Scroll To Top Button With jQuery (cherrysave.com) [...]

Post a Comment

Your email is never shared. Required fields are marked *

*
*