Created
April 24, 2018 08:04
-
-
Save paullacey78/6e95df33c29a682e48f6aed1edddecde to your computer and use it in GitHub Desktop.
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
// From https://wpbeaches.com/anchor-link-offset-to-other-pages-with-fixed-header-size/ | |
jQuery(document).ready(function($) { | |
// set a variable for the anchor link which is the location.hash | |
var anchorLink = $(window.location.hash); | |
// test to see if the link is a anchor link, if not the length will have no value, this is done to avoid js errors on non anchor links | |
if ( anchorLink.length ) { | |
// set an element as the fixed entity, header in this case and get its height | |
var offsetSize = $("header").innerHeight(); | |
// fire the animation from the top of the page to the anchor link offsetting by the fixed elements height, the number is the speed of the animation | |
$("html, body").animate({scrollTop: anchorLink.offset().top - offsetSize }, 500); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment