Created
September 8, 2012 19:57
-
-
Save galaakk/3679211 to your computer and use it in GitHub Desktop.
jQuery : Scroll to anchor
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
$(document).ready(function() { | |
// Click event for any anchor tag that's href starts with # | |
$('a[href^="#"]').click(function(event) { | |
// The id of the section we want to go to. | |
var id = $(this).attr("href"); | |
// An offset to push the content down from the top. | |
var offset = 60; | |
// Our scroll target : the top position of the | |
// section that has the id referenced by our href. | |
var target = $(id).offset().top - offset; | |
// The magic...smooth scrollin' goodness. | |
$('html, body').animate({scrollTop:target}, 500); | |
//prevent the page from jumping down to our section. | |
event.preventDefault(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment