A Lightweight AutoScroll to Top jQuery Plugin
Increasing your webpage usability is not that easy, you have to consider a lot of things. One of the necessary element is to be able the user to easily back to the top most of the page especially when the content is long enough. This plugin will add an elegant scroll top effect and functionality to you page in just a few line of JavaScript.

The Plugin
(function(jq) {
jq.autoScroll = function(ops) {
ops = ops || {};
ops.styleClass = ops.styleClass || 'scroll-to-top-button';
var t = jq('<div class="'+ops.styleClass+'"></div>'),
d = jq(ops.target || document);
jq(ops.container || 'body').append(t);
t.css({
opacity: 0,
position: 'absolute',
top: 0,
right: 0
}).click(function() {
jq('html,body').animate({
scrollTop: 0
}, ops.scrollDuration || 1000);
});
$(window).scroll(function() {
var sv = d.scrollTop();
if (sv < 10) {
t.clearQueue().fadeOut(ops.hideDuration || 200);
return;
}
t.css('display', '').clearQueue().animate({
top: sv,
opacity: 0.8
}, ops.showDuration || 500);
});
};
})(jQuery);
Plugin in use
$(document).ready(function(){
$.autoScroll({
scrollDuration: 2000,
showDuration: 600,
hideDuration: 300
});
});
The CSS
A CSS code required for the plugin. In your implementation you need to change the location of the image.
.gp{
background: #666 url(arrow_up.png) center center no-repeat;
width: 32px;
height: 32px;
color: #fff;
font-family: verdana;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-o-border-radius: 5px;
cursor: pointer;
padding: 15px;
margin: 5px;
}
View DEMO


20. Aug, 2010












It’s a wonderful script. Thank you very much.
I have a problem thought. It works fine in every other browser except IE.
I would appreciate your feedback.
Thank you in advance,
Yannis
IE doesn’t capture scroll event on document object so use
$(window).scroll(function() {
instead of
d.scroll(function() {
Checkout latest code at http://jsbin.com/udoci/6/edit
Hey Yannis,
Thanks for pointing out I’ve already fixed this bug.
Change the line (line E)
d.scroll(function() {
to
$(window).scroll(function(){
IE doesn’t capture scroll events on document object.
You can checkout the real demo here
http://jsbin.com/udoci/6/edit
Hi MaXPert,
You are a machine! I checked this out and it works really fine now.
Let me point out the fact that it is not line E. It is line T… and please let me suggest that you will have to correct the code above so that other developers will not have to check out the comments to find out about the problem.
ThankS,
Yannis
Really IE is a headache! Great tutorial indeed!
Grate tutorial, I am currently in process of implementing in my site. Thanks for sharing.
Hi, I have this working just great on my inside pages, but not on my index page, which also uses mootools for milkbox.js. Any ideas of a way to make this work along with the mootools library? Might it have to do with the order in which I load the js libraries and the scripts?
TIA
I’m a complete ROOKIE. Can Somebody tell me how or where to place the code for
“The Plugin”
“Plugin in use”
I know the place for this one “The CSS”
Complete details please.
Sorry
Nice tutorial…i was for something like this for one of my website. Goingo to try to build it in right now..thx for a great post.