Created
December 31, 2012 01:44
-
-
Save linuxbender/4416739 to your computer and use it in GitHub Desktop.
Javascript: ScrollToTop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="#" class="topLink"></a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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