我正在使用 goToByScroll 脚本,我需要它忽略我页面的前 40 像素,因为我有一个固定的导航,导致所有位置都太低 40 像素。
这是我的代码:
<script>
function goToByScroll(id){
$('html,body').animate({scrollTop: $("#"+id).offset().top},'500');
}
</script>
有什么解决办法吗?
最佳答案
计算偏移量时只需包括 40 个像素...
$('html,body').animate({scrollTop: $("#"+id).offset().top - 40},'500');
或者可能是这样的……
$('html,body').animate({scrollTop: $("#"+id).offset().top - $("#nav").height() },'500');
虽然@Praveen 的回答是错误的,但他在其中放置了一个很好的 .stop()
以防止多次触发相互干扰。一切都在这里......
<script>
// assuming `#nav` identifies your navigation element...
function goToByScroll(id){
$('html,body').stop().animate({scrollTop: $("#"+id).offset().top - $("#nav").height() },'500');
}
</script>
关于javascript - jQuery goToByScroll 偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7043200/