Due to a number of requests, I'm writing a detail tutorial on how to create an animated scroll to top as seen on Web Designer Wall. It is very simple to do with jQuery (just a few lines of code). It checks if the scrollbar top position is greater than certain value, then fade in the scroll to top button. Upon the link is clicked, it scrolls the page to the top. View the demo to see it in action.
Design & CSS
Declare the #back-top elment with position:fixed so it stays fixed on the page. The span tag is optional. I added the span tag to display the arrow graphic. I also added transition:1s (1s = 1 second) to create the fading effect on mouse over.
Take a look at this step demo (note: the jQuery part is not implemented yet).
#back-top {
position: fixed;
bottom: 30px;
margin-left: -150px;
}
#back-top a {
width: 108px;
display: block;
text-align: center;
font: 11px/100% Arial, Helvetica, sans-serif;
text-transform: uppercase;
text-decoration: none;
color: #bbb;
/* transition */
-webkit-transition: 1s;
-moz-transition: 1s;
transition: 1s;
}
#back-top a:hover {
color: #000;
}
/* arrow icon (span tag) */
#back-top span {
width: 108px;
height: 108px;
display: block;
margin-bottom: 7px;
background: #ddd url(up-arrow.png) no-repeat center center;
/* rounded corners */
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
/* transition */
-webkit-transition: 1s;
-moz-transition: 1s;
transition: 1s;
}
#back-top a:hover span {
background-color: #777;
}
jQuery Part (Demo)
Below is the Javascript code (you can paste it any where on the page). Basically, it hides the #back-top element (it is the <p id="back-top"> tag in my demo) initially . Then it checks if the window scrollbar top position (scrollTop) is greater 100, then fade in the #back-top element, else fade out. The next set of code is the click function. If the #back-top link is clicked, it will animate the body scrollTop to 0.
If you want to learn some basic jQuery coding, read my jQuery for Designers tutorial.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
// hide #back-top first
$("#back-top").hide();
// fade in #back-top
$(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 100) {
$('#back-top').fadeIn();
} else {
$('#back-top').fadeOut();
}
});
// scroll body to 0px on click
$('#back-top a').click(function () {
$('body,html').animate({
scrollTop: 0
}, 800);
return false;
});
});
});
</script>
No Javascript Fallback
Note the back to top button is linking to anchor #top which is the ID of the <body> tag. Technically speaking you don't need to assign any anchor link because jQuery can scroll the page to any position. However, it is nice to include it because it provides a fallback if Javascript is not supported.
Nice tutorial.. Thank you very much…
Hi,
You are a wiz with scripting, and the more I am reading your site the more I feel my own scripting skills are growing. Now, I know you offer all these files for free and I am very grateful for all you are giving back to the community, but could you possibly provide a couple of free templates I could test this script in? No, offense the headings/subheadings HTML file is not what I would call advanced.
I am asking this, because I want to see how it fares in a full design template with CSS, images and the rest.
Also do you think this could work on handheld devices?
“Also do you think this could work on handheld devices?”
I use this on a lot of websites. I’ve found that it doesn’t work well on my particular Android phone (1st generation Droid). Once the ‘top’ image appears it stays put there and not at the bottom of the screen.
Also, it seems that the latest versions of FF and Opera don’t hide it on load, as the javascript would imply.
“Also, it seems that the latest versions of FF and Opera don’t hide it on load, as the javascript would imply.”
Instead of hiding #back-top with jQuery, you can “hide” it, with css:
#back-top{
display: none;
}
And now you can remove $(“#back-top”).hide(); from the js.
Thanks for your tutorial btw!
Is it possible to implement this on joomla 2.5 site. Any help is welcome. I write to Peter but I understand he is busy!! THX in advance
I just wanted to say thank you and let you know that I will be using your solution on http://www.closedfortheholiday.com Thanks again!
This is awesome. I was playing around with another tutorial and there was additional javascript and seemed more code than needed. Love that you explained it & I can use my own image with or without text. Thanks!
This is awesome.Thanks for the great work.
Great work.. Thanks
Nice tutorial! Thanks for sharing this.
I think the button should appear next to the scrollbar because this is where the mouse of the user is often, when she is scrolling on the page. Therfore I think the right bottom corner of the page is a good position for this link to the top pf the page.
Actually it depends on the design of the page. I just saw your link here on this page and it makes sense – where it is. ;)
Nice tutorial dude…
nice tutorial
Very Good Script. Thanks you very much for sharing it.
Awesome tutorial! Very easy to use :)
Thanks for sharing!
Nice, great code, this is absolutely what I’m looking for:)
thanks!
Thanks for the tutorial, just what I wanted
Excellent!
why its not visible on ie7
That was amazing–thanks so much! :) Mine doesn’t wait until the scroll value is >100 to show up, though. It’s just there once you load the site. I mean, it’s not a big deal or anything, and I’m super grateful you put this up! But I was just wondering if it’s ’cause of something I did wrong?
“Also, it seems that the latest versions of FF and Opera don’t hide it on load, as the javascript would imply.”
“Instead of hiding #back-top with jQuery, you can “hide” it, with css:
#back-top{
display: none;
}
And now you can remove $(“#back-top”).hide(); from the js.”
If I try to hide it with css (by “display: none;”) , button never appears even at the bottom of the page. In the tutorial version, on the contrary, it always appears as soon as the page is loaded.
Any help would be appreciated
This is sizzling concepts and i appreciate your help by mentioning the programming code…cheers…