Created
April 1, 2016 15:22
-
-
Save dbridgman/eaddf9cd515430a09cdf300064fb5b6a to your computer and use it in GitHub Desktop.
A simple jQuery script that will allow for smooth scrolling on a page when clicking on an anchor point.
This file contains 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
<!-- ENSURE BELOW PASTES DIRECTLY INTO THE HEAD OF THE PAGE --> | |
<!-- SMOOTH SCROLL --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
<script> | |
$(function() { | |
$('a[href*=#]:not([href=#])').click(function() { | |
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { | |
var target = $(this.hash); | |
target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); | |
if (target.length) { | |
$('html,body').animate({ | |
scrollTop: target.offset().top | |
}, 1000); | |
return false; | |
} | |
} | |
}); | |
}); | |
</script> | |
<!-- End of SMOOTH SCROLL --> | |
<!--- HTML EXAMPLE BELOW --> | |
<a href="#point"></a> | |
<div name="point"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment