我有一个带有滚动条的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");
我是初学者,我首先在这里搜索,然后猜出了代码
您使用的值不正确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] 删除。
我来说两句