Created
March 26, 2014 07:37
-
-
Save pingwping/9778376 to your computer and use it in GitHub Desktop.
display go-to-top button automatically
This file contains hidden or 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
<style TYPE="text/css"> | |
<!-- | |
#go-to-top { | |
display: none; | |
position: fixed; | |
right: 60px; | |
bottom: 100px; | |
padding: 18px; | |
color: #666; | |
background: #DDD; | |
font: 36px/18px Helvetica,Arial,Verdana,sans-serif; | |
opacity: 0.7; | |
outline: 0 none; | |
text-decoration: none; | |
text-shadow: 0 0 1px #DDD; | |
vertical-align: baseline; | |
-moz-border-radius: 5px; | |
-khtml-border-radius: 5px; | |
-webkit-border-radius: 5px; | |
border-radius: 5px; | |
} | |
--> | |
</style> | |
... | |
<footer> | |
<a style="display: none; " rel="nofollow" href="#top" id="go-to-top">▲</a> | |
</footer> |
This file contains hidden or 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(){ | |
$("#go-to-top").click(function(){ | |
$("html, body").animate({'scrollTop': 0}, 400); | |
return false; | |
}); | |
$(window).scroll(function() { | |
var top = $(document).scrollTop(); | |
var g = $("#go-to-top"); | |
if (top > 300 && g.is(":hidden")) { | |
g.fadeIn(); | |
} else if(top < 300 && g.is(":visible")) { | |
g.fadeOut(); | |
} | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment