Free up your time and revolutionize your design with simple web development.

Create a Hovering Scroll to Top Button With JQuery

Posted: November 5th, 2009 | Author: Kurt | Filed under: JQuery | 6 Comments »
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.


6 Comments on “Create a Hovering Scroll to Top Button With JQuery”

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

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

  2. 2 Cañolimón said at 11:02 am on November 9th, 2009:

    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.

  3. 3 7 ‘Scroll to Top’ jQuery Solutions said at 4:28 pm on February 21st, 2010:

    [...] 3. Disappearing “Scroll to top” link with jQuery and CSS [...]

  4. 4 Web Resources » 7 λύσεις για ‘Scroll To Top’ με jQuery said at 9:30 am on March 6th, 2010:

    [...] Δημιουργήστε ένα αιωρούμενο Scroll To Top κουμπί με jQuery [...]

  5. 5 Annie said at 12:02 am on March 9th, 2010:

    This was great, thank you for the tutorial. I had been looking for awhile for an easy way to do a scrolling object. I am fairly unfamiliar with JQuery, but this was perfectly straight-forward. Thank you!

  6. 6 jQuery Resources for One-Page Portfolios | pro2go Designs Blog said at 11:59 am on March 9th, 2010:

    [...] Create a Hovering Scroll to Top Button with jQuery Because one-page sites tend to be rather long, it’s helpful to have a link to allow visitors to easily return to the top of the page. Cherrysave has a tutorial that shows how to create a button that will hover as the user scrolls. [...]


Leave a Reply