Skip to content

Instantly share code, notes, and snippets.

@alyssachanspace
Created February 2, 2017 18:20
Show Gist options
  • Save alyssachanspace/a66843b672db7ec3ced7fd69f8a746cd to your computer and use it in GitHub Desktop.
Save alyssachanspace/a66843b672db7ec3ced7fd69f8a746cd to your computer and use it in GitHub Desktop.
jQuery script of Back to Top button
<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