Created
May 16, 2013 16:14
-
-
Save rafalborowski/5592944 to your computer and use it in GitHub Desktop.
Scroll to top button. Create an empty div in the footer with a "go-top" class.
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
| /* Scroll to top */ | |
| jQuery(document).ready(function(){ | |
| jQuery('nav ul li a').click(function(){ | |
| var el = jQuery(this).attr('href'); | |
| var elWrapped = jQuery(el); | |
| scrollToDiv(elWrapped,40); | |
| return false; | |
| }); | |
| function scrollToDiv(element,navheight){ | |
| var offset = element.offset(); | |
| var offsetTop = offset.top; | |
| var totalScroll = offsetTop-navheight; | |
| jQuery('body,html').animate({ | |
| scrollTop: totalScroll | |
| }, 500); | |
| } | |
| }); | |
| /* Show/Hide back to top button */ | |
| jQuery(document).ready(function(){ | |
| jQuery(window).scroll(function(){ | |
| if (jQuery(this).scrollTop() > 200) { | |
| jQuery('.go-top').fadeIn(300); | |
| } else { | |
| jQuery('.go-top').fadeOut(300); | |
| } | |
| }); | |
| jQuery('.go-top').click(function(event){ | |
| event.preventDefault(); | |
| jQuery('html, body').animate({ | |
| scrollTop: 0 | |
| }, 300); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment