Skip to content

Instantly share code, notes, and snippets.

@alyssachk
Created September 11, 2016 06:38
Show Gist options
  • Save alyssachk/5c337d0e5f89477851583c2ccfa89e30 to your computer and use it in GitHub Desktop.
Save alyssachk/5c337d0e5f89477851583c2ccfa89e30 to your computer and use it in GitHub Desktop.
Back to Top Button jQuery
<a href="#" class="back-to-top">回到頁首</a>
<script>
jQuery(document).ready(function() {
var offset = 220;
var duration = 500;
jQuery(window).scroll(function() {
if (jQuery(this).scrollTop() > offset) {
jQuery('.back-to-top').fadeIn(duration);
} else {
jQuery('.back-to-top').fadeOut(duration);
}
});
jQuery('.back-to-top').click(function(event) {
event.preventDefault();
jQuery('html, body').animate({scrollTop: 0}, duration);
return false;
})
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment