简体   繁体   English

如何滚动到溢出的div的底部或顶部

[英]How to scroll to the bottom or top of a div with overflow

I have some div with a lot of content, i want to scroll some divs inside the main div into view. 我有一些div有很多内容,我想在主div中滚动一些div进入视图。 so this is mainly the y-overflow scroll with x-overflow hidden. 所以这主要是隐藏了x溢出的y溢出滚动。

I don't want to scroll the window or body but only the div's contents. 我不想滚动窗口或正文,只想滚动div的内容。

$(document).on("click", ".CommentBut", function() {
 //////when the Comment button is clicked.////
 $('#QuickRead').scrollTop()
////Scroll Quick Read Div to the top./////
});

But the above is not working and i think probably there's something wrong with my method or syntax. 但上面的工作没有用,我想我的方法或语法可能有问题。

Use following to goto top , here take dom element by ID, then do scrolling ,1st parameter is where to scroll (in pixels) ,2nd is time to scrolling. 使用以下转到顶部,这里按ID取dom元素,然后滚动,第一个参数是滚动的位置(以像素为单位),第二个是滚动时间。

jQuery("#QuickRead").animate({scrollTop: 0}, 1);

To goto bottom : 转到底部:

  var $target = jQuery("#QuickRead"); 
  $target.animate({scrollTop: $target.height()}, 1000);

well you can say its a kind of bug. 那么你可以说它是一种bug。 Some browsers apply the "overall" scroll to document.documentElement (the element) and others to document.body (the element). 有些浏览器将“整体”滚动应用于document.documentElement(元素),将其他滚动应用于document.body(元素)。 For compatibility with both, you have to apply the scrolling to both.. 为了兼容两者,您必须将滚动应用于两者。

try below thing 尝试下面的事情

if($.browser.safari) bodyElement = $("body")
else bodyElement = $("html,body")

bodyElement.scrollTop(100)

update : 更新:

$("#QuickRead").animate({scrollTop:$('#div_id').position().top}, 'slow');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM