LoginSignup
1
1

More than 5 years have passed since last update.

headerをposition fixedした時に同一ページ内のリンクがずれる

Last updated at Posted at 2016-04-16

headerをfixedでTOPに固定した際に、同一ページへのリンクを設定すると、headerの高さ分だけスクロールがずれてしまう。

sample.js

$(function(){
  var headerHeight = $('header').height();
  $('a[href^=#]').on('click',function(){
    var href = $(this).attr('href');
    var target = $(href == '#' || href == '' ? 'html' : href);
    var position = target.offset().top - headerHeihgt;
    $('html,body').animate({scrollTop:position}, 550, 'swing');
    return false;
  });
});

これで解決。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1