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.
Hi
It doesn’t slide in Opera!
Absolutely fantastic! Thanks a lot for sharing this.
It’s very cool the effect. It’s also very clever that you are doing the fade-in fade-out also in JQuery and not only via CSS3 transition.
Thanks for this I did something similar on an old site but made the page go to the right when you clicked on a link it was quite cool but took ages to load because the hole site was on one page
Hmmm, since implementing this some weird things happen with my YouTube embedded video’s.
If I scroll down, the arrow appears, all good. But if I scroll to top, my youtube video stops? That’s odd! I’m really sorry, I cannot share a link due to the site still being in development, but if you feel like ‘wtf?! I wanna help’, you can email me on jasper@eclecticradio.nl :)
Thank for a nice script. I’ve already put it on my page:). I’ve tested it under Opera 11.10 and it works fine.
Glad you likey. I didn’t write it, it was written by Matthew Ruten. Look for us both on Linked In!
Oooops. Wrong post :P
looks good. pretty simple..
although it’s similar to what I did a couple of months ago.
http://typicalwhiner.com/tutorials-and-downloads/effortless-jquery-floating-back-to-top-script-v2/
thanks for you. translated your post to russian on my blog ;)
The code is nice and works properly. We are a website design company that specializes in Web Design for Doctors Only. Come check out our site when you get a chance. Thanks
Thank’s a lot! I’ll add it on my blog.
web designer wall is a great page!
This is great … but on a similar topic does anyone know a good demo for a smooth scroll using java going down the page with links at the top … Something that I can just copy paste, my code hand is weak? I did a google search and the examples were as easy as I thought.
Many thanks,
Jay
I’m using it .
I really needed some help with my “Animated scroll to top” site I’m building so this tutorial couldn’t have come sooner! Thanks!
Recent surveys, children of depressed mothers’ negative patterns of activity occurring in different brain reveals. This is for children of mothers who take more risks in the future is going to have depression.
fuu bar
This certainly does the trick, but the only problem I find with this method is that it takes 800 milliseconds to animate the scroll, so if you’re close to the top, the animation is leisurely, and if you’re at the footer of a very long page, the animation is completely indistinguishable. I’m sure there has to be a way to calculate the page length and use a derivative number as the scroll speed, right?
Dont work in Opera. :(
didn’t work for me also, but only 2 days, and then… voila! (may it be an update? 11.10)
Felicitaciones por el tutorial. No lo conocía y además de vistoso (como lo es todo lo de Jquery) es sumamente útil.
Muchísimas gracias por el aporte
Wow… amazing, I have searching this kind of script…
looks great and works great apart from on an ipad :(
http://crab-tree.com is now a worldwide web design services website