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.
adsadsadad
Its great tutorial. i love you so much.
Too bad it doesn’t work in IE :(
Thank you so so so much for this!! <3 c:
Love the script! It was really easy to use and falls back nicely to quirks mode in IE. It’s not beautiful in IE, but what is???
I added a little opacity to soften it a bit and added display:none; to #back-top css. Why you ask? It was showing for a split second when the page loaded. I guess that is because my script was below the markup???
Also, I wonder how this renders in IE6 since we have position: fixed…
Thanks again, love this site!
James Weaver
Very cool, and very easy to use.
Thank you so much!
I want use it in many of my project!
;)
Nice, very simple logic & cool logic.. :-)
Vielen vielen Dank für dieses Tutorial. Nach solch einer simplen Lösung habe ich gesucht!
Thank you so much for this tutorial. After such a simple solution I was looking for!
Worked perfect, thanks!
Cool, but where do i put it ? before/after what code ? please answer me on my blog or e-mail.
hi, i tried using the code above but the #back-top shows.
can you pls .figure out whats wrong with this code?
#back-top {
position: fixed;
bottom: 30px;
margin-left: -150px;
}
#back-top a {
width:100px;
display: block;
img-align: center;
/*transition*/
-webkit-transition: 1s;
-moz-transition: 1s;
transition: 1s;
}
#back-top a:hover {
color: #000;
}
/*arrow icon(span tag)*/
#back-top span {
width: 100px;
height: 100px;
dispaly: block;
margin-bottom: 6px;
background: #ddd url (arrow.jpg) no-repeat left left;
/*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;
}
$(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;
});
});
});
im really confused … O.o
Has this been tested on responsive sites and tablets/phones?
the script works fine in ff, thanks
Thanks for share it
Thanks dear, this is very useful and easy to use way for scroll to top button.
I was looking for a hidden menu tutorial that would show my menu on scroll. I could not find one that I liked.
I made some very minor modifations to your script, a little CSS tweaking to the navigation element ID on my site and it gave me just what I wanted for my hidden menu to show on scroll.
I made a tutorial that is gives you credit for this awesome code and how to make it work as a hidden menu. You should be seeing traffic from that tutorial.
Thanks for all the hard work and sharing with us all.
thank you so much!!!
thank you so much for sharing this information. it is very useful for us.
Thank you sir! You saved my days! I love your tutorials!!!!
Does anyone have a solution to make this work in IE6? I know how much that browser hates position:fixed… :)
Any help would be much appreciated.