滚动DIV元素的特定滚动条

奈娜·卡普尔(Naina Kapoor)

我有一个带有滚动条的div元素,我只想在某个事件中在顶部或底部滚动此滚动条

<div id="scroll_box" style="height:300px; overflow:auto;">
 <pre>
  Top
  .
  .
  .
  .
  .
  .
  .
  .
  .
  .
  Bottom
 </pre>
</div>
<button id="go_top">Top</button>
<button id="go_bottom">Bottom</button>

我不希望主滚动条(Windows)移动

我尝试过

$('#scroll_box').animate({scrollTop: $("#scroll_box").offset().top},"fast");
$('#scroll_box').animate({scrollBottom: $("#scroll_box").offset().bottom},"fast");

我是初学者,我首先在这里搜索,然后猜出了代码

罗里·麦克罗森(Rory McCrossan)

您使用的值不正确scrollTop,并且scrollBottom不存在。另外,您需要将这些语句附加到click每个按钮事件上,否则它们都在加载时运行并相互抵消。试试这个:

$('#go_top').click(function() {
    $('#scroll_box').animate({scrollTop: 0 },"fast");
});
$('#go_bottom').click(function() {
    $('#scroll_box').animate({scrollTop: $("#scroll_box pre").height() },"fast");
});

小提琴的例子

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章