Skip to content

Instantly share code, notes, and snippets.

@bocharsky-bw
Created April 21, 2014 19:22
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 bocharsky-bw/11153421 to your computer and use it in GitHub Desktop.
Save bocharsky-bw/11153421 to your computer and use it in GitHub Desktop.
To top button on JavaScript with jQuery library
<a id="bw_to_top" href="#top" style="
position: fixed;
left: 20px;
bottom: 20px;
display: block;
opacity: 0;
" title="To top">To top</a>
<script type="text/javascript">
var obj = $('#bw_to_top'); // object of to_top button
var offset = 300; // offset from the current position of to_top button
var frozen = true; // flag that show is show to_top button
$(window).scroll(function(){
var showed = obj.offset().top - offset > $(window).height();
if (showed == true && frozen == true) {
// show to_top button
frozen = false;
obj.stop().animate({'opacity': 1}, 800);
} else if ( showed == false && frozen == false) {
// hide to_top button
frozen = true;
obj.stop().animate({'opacity': 0}, 800);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment