Skip to content

Instantly share code, notes, and snippets.

@k-ish
Created October 10, 2018 05:59
Show Gist options
  • Save k-ish/c3582167ad50c7ab58d48b042eeb165f to your computer and use it in GitHub Desktop.
Save k-ish/c3582167ad50c7ab58d48b042eeb165f to your computer and use it in GitHub Desktop.
ヘッダー固定時のページ内リンクずれの位置修正
$(function(){
$('nav li a, .menu li a').click(function() {
var speed = 400;
var href= $(this).attr("href");
var target = $(href == "#" || href == "" ? 'html' : href);
var headerHeight = 100; //固定ヘッダーの高さを指定
var position = target.offset().top - headerHeight; //ターゲットの座標からヘッダの高さ分引く
$('body,html').animate({scrollTop:position}, speed, 'swing');
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment