Effortless jQuery Floating Back-To-Top Script v2
I’ve made a simple script (Effortless jQuery Floating Back-To-Top) a month ago and one of my readers was wondering if I can make the button hidden and will just show if its scrolled down a bit. So, here it is. Enjoy ![]()
HTML
place this inside the body of your web page. before the < /body > tag.
<div id="backtotop">
<a href="#">
<img src="your-image-folder/back_to_top_btn.jpg" border="0" alt="Back to TOP" />
</a>
</div>
CSS
place this inside the head of your web page. before the < /head > tag.
<style type="text/css">
#backtotop {
position: fixed;
right:0px;
bottom:15px;
display:none;/*hid the button first*/
}
#backtotop a {
text-decoration:none;
border:0 none;
display:block;
width:31px;
height:155px;
}
#backtotop a:hover {
opacity:.8; /*mouse over fade effect*/
}
.clrb {
clear:both;
}
#maindiv {
width:700px;
border:1px solid #999;
margin:auto;
padding:20px;
}
</style>
JS
place this inside the head of your web page. before the < /head > tag.
<!-- include jQuery Library -->
<script type="text/javascript" src="js/jquery.js"></script>
<!-- efforless script below -->
<script type="text/javascript">
jQuery(document).ready(function(){
var pxShow = 300;//height on which the button will show
var fadeInTime = 1000;//how slow/fast you want the button to show
var fadeOutTime = 1000;//how slow/fast you want the button to hide
var scrollSpeed = 1000;//how slow/fast you want the button to scroll to top. can be a value, 'slow', 'normal' or 'fast'
jQuery(window).scroll(function(){
if(jQuery(window).scrollTop() >= pxShow){
jQuery("#backtotop").fadeIn(fadeInTime);
}else{
jQuery("#backtotop").fadeOut(fadeOutTime);
}
});
jQuery('#backtotop a').click(function(){
jQuery('html, body').animate({scrollTop:0}, scrollSpeed);
return false;
});
});
</script>


Hi there.
Simple and beautiful!! Much appreciated. Only one question though. I would like to position the link along the right side of the global div in which the backtotop-div is nestet, instead of the right side of the body-element. Is that possible? And if so, how? I don’t think I’m able to absolutely position my global- div as I use “margin: 0 auto” to center the site on the screen. Thanks!
/Claus
Hi Claus,
First of all, thank you. 8)
Not sure what you mean though but if the button position is your concern, you can manipulate where you want the button to show (left or right) by just changing the values of the CSS. Hope this helps.
I saw your post regarding another back-to-top jquery script. I really like this one and can’t wait to implement it. Thanks for sharing.
Thanks Kathy, glad you liked it..
No worries, always happy to help others.
Cheers!
nice tutorial, keep up the good job ^^
Thank you.
Very simple and functional. Thanks!
thank you
Love it. Works so well and so easy to install. Love how you combined fade in/out feature when close to the top and the floating scrolling up effect rather than “jump” when you click on the button.
Have same query as Claus: Would like to have the button to scroll alongside my web page as if attached to the page as in http://www.starzmart.com/ Have done what you suggested and changed the values in the css but the placement changes in different browsers and on different sized screens. Is there a way of fixing the placement no matter which browser or screen?
Hi there Kara,
Happy new year.
Thanks for using my script.
For your query, I think you can achieve this by tweaking the HTML and CSS.
HTML: place the ‘backtotop’ div inside your global-div (what Claus said; I now understand what Claus meant, thanks Kara
CSS: #backtotop, remove the ‘right’ attribute and add/use ‘padding-left’ for the placement.
Hope this helps
Happy New Year to you too! Thanks so much for your very quick reply. Much appreciated. You are brilliant! That has worked perfectly. It looks so much better and I feel will be more prominent and noticed by viewers of the page in my case as I have used a more subtle image than yours to blend with my website.
Thank you, thank you!!
I hope that Claus is receiving this post so he can now do the same.
Cheers
Kara
Cool, glad to hear that Kara..
No worries.. Always glad to help..