Skip to content

Instantly share code, notes, and snippets.

@linuxbender
Created December 31, 2012 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxbender/4416739 to your computer and use it in GitHub Desktop.
Save linuxbender/4416739 to your computer and use it in GitHub Desktop.
Javascript: ScrollToTop
.topLink {
background: url('http://jquerymobile.com/demos/1.2.0/docs/toolbars/glyphish-icons/88-beermug.png') no-repeat;
position:fixed;
width: 30px;
height: 30px;
bottom:30px;
right:20px;
}
<a href="#" class="topLink"></a>
$(function() {
$.fn.scrollToTop = function() {
$(this).hide().removeAttr("href");
if ($(window).scrollTop() != "0") {
$(this).fadeIn("slow")
}
var scrollDiv = $(this);
$(window).scroll(function() {
if ($(window).scrollTop() == "0") {
$(scrollDiv).fadeOut("slow")
} else {
$(scrollDiv).fadeIn("slow")
}
});
$(this).click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow")
})
}
});
$(function() {
$(".toplink").scrollToTop();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment