开发者

jQuery goToByScroll offset

开发者 https://www.devze.com 2023-03-27 13:41 出处:网络
I\'m using the goToByScroll script and I need it to ignore the first 40px of my page because I have a fixed navigation, resulting in all positions being 40px too low.

I'm using the goToByScroll script and I need it to ignore the first 40px of my page because I have a fixed navigation, resulting in all positions being 40px too low.

Here's my code:

<script> 
  function goToByScroll(id){
  $('html,body').animate({scrollTop: $("#"+id).offset().top},'500');
  }
</script>

Any solu开发者_JAVA百科tions?


Just include the 40 pixels when calculating the offset...

$('html,body').animate({scrollTop: $("#"+id).offset().top - 40},'500');

Or maybe something like this...

$('html,body').animate({scrollTop: $("#"+id).offset().top - $("#nav").height() },'500');

And although @Praveen's answer is wrong, he puts a nice .stop() in there to prevent multiple firing from interfering with each other. Here it is alltogether...

<script>
  // assuming `#nav` identifies your navigation element...
  function goToByScroll(id){
      $('html,body').stop().animate({scrollTop: $("#"+id).offset().top - $("#nav").height() },'500');
  }
</script>


 $('html, body').stop().animate({ scrollTop: 40 }, 500);


<script> 
  function goToByScroll(id){
    $('html,body').animate({scrollTop: $("#"+id).offset().top + 40},'500');
  }
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消