我有一个动态显示内容的固定大小的 div。
如果内容对于 div 来说太大,我希望 div 的内容开始自行滚动,以便可以看到所有内容。
现成的解决方案似乎强制内容始终滚动,无论它是否适合 div。
谢谢。
最佳答案
除了 Shahar 提到的,您还可以使用 jQuery animate api 滚动到 div 的底部。
var dynamicDiv = $("#dynamic_div");
scrollHeight= dynamicDiv[0].scrollHeight;
divHeight = dynamicDiv.height();
if(scrollHeight > divHeight){
pageScrolls = scrollHeight/divHeight;
$("#dynamic_div").animate({
scrollTop: scrollHeight // scroll till the end of the div
}, 1500 * pageScrolls); // adjust the time based on how much scrolling needs to be done
}
这是 jsfiddle
关于javascript - 如果滚动条可见,则滚动内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31681551/