0

所以我使用这个脚本滚动到 JQuery 中的一个 div:

jQuery(document).ready(function($) {

$(".scroll").click(function(event){     
    event.preventDefault();
    $('html,body').animate({scrollTop:$(this.hash).offset().top}, 800);
});
});

问题是当它滚动到一个 div 时,标题(比如前 20px)是隐藏的,因为在我的页面上实现了一个标题顶部栏 div。我将如何控制滚动使其提前停止以便您可以查看整个 div。

4

3 回答 3

0

尝试使用

$('html, body').animate({scrollTop:$('#your_custom_position_id').position().top}, SPEED);
于 2013-06-11T12:49:24.900 回答
0

使用您已有的同一行:

$('html,body').animate({scrollTop:$(this.hash).offset().top}, 800);

你只需要发挥scrollTop价值。例如

$('html,body').animate({scrollTop:$(this.hash).offset().top - 50}, 800);
于 2013-01-16T20:04:43.727 回答
0

您可以添加标题的大小,使其停止在该位置。

jQuery(document).ready(function($) {

  $(".scroll").click(function(event){     
    event.preventDefault();
    $('html,body').animate({scrollTop:$(this.hash).offset().top+20}, 800);
  });
});
于 2013-01-16T20:03:48.010 回答