-
-
Save rqx110/4fc416cf9610aab9c044 to your computer and use it in GitHub Desktop.
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
/** | |
* 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