Skip to content

Instantly share code, notes, and snippets.

@rqx110
Forked from jeremyboggs/back-to-top.js
Created June 30, 2014 05:41
Show Gist options
  • Save rqx110/4fc416cf9610aab9c044 to your computer and use it in GitHub Desktop.
Save rqx110/4fc416cf9610aab9c044 to your computer and use it in GitHub Desktop.
/**
* Show or hide the button depending on the scroll position.
*/
function animateButton() {
var button = $('#back-to-top');
var scrollPosition = $(window).scrollTop();
if (scrollPosition > 400) {
button.fadeIn();
} else {
button.fadeOut();
}
}
/**
* Create the button and append it to the body.
*/
$(function () {
$('<a id="back-to-top">Back to Top</a>')
.click(function () {
$('html,body').animate({
scrollTop: 0
}, 1200);
return false;
})
.appendTo($('body'));
});
/**
* Run the animateButton function on window resize, scroll, and load.
*/
$(window).resize(animateButton);
$(window).scroll(animateButton);
$(window).load(animateButton);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment