Skip to content

Instantly share code, notes, and snippets.

@erochest
Forked from jeremyboggs/back-to-top.js
Last active December 11, 2015 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erochest/4586941 to your computer and use it in GitHub Desktop.
Save erochest/4586941 to your computer and use it in GitHub Desktop.
(function(window, $, undefined) {
var $window = $(window);
/**
* 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)
.scroll(animateButton)
.load(animateButton);
})(window, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment