简体   繁体   English

div上的自动滚动不起作用

[英]Auto-scroll on div doesn't work

I've got a CSS problem that I can't deal with since I'm very bad at it. 我有一个无法解决的CSS问题,因为我对此很不好。 Here is a fiddle I made to show you the issue. 这是我给您展示的小提琴 When you click the send button several times you can see that the <div> doesn't autoscroll and that messages are hidden. 多次单击发送按钮时,您会看到<div>不会自动滚动,并且消息已隐藏。

I tried something like this: 我尝试过这样的事情:

var objDiv = document.getElementById("mainblock");
objDiv.scrollTop = objDiv.scrollHeight;

It stills does the same (like in the fiddle). 剧照也一样(就像在小提琴中一样)。

This happens because your scroll is actually on the Body element 发生这种情况是因为您的滚动实际上在Body元素上

try using this: 尝试使用此:

var objDiv = document.getElementsByTagName("body")[0];    
objDiv.scrollTop = objDiv.scrollHeight;

Example

On the Other Hand You can use your javascript with this css added to your app: 另一方面,您可以将JavaScript与此CSS添加到您的应用中使用:

html,body {
  height: 100%;
   overflow-y:hidden;
}

.container {
  padding: 40px 20px;
  margin: 0 auto;
  max-width: 100%;
  overflow-y:auto;
    max-height: calc(100% - 150px);
}

Example2 例题

Try the following: 请尝试以下操作:

JS Fiddle DEMO: JS Fiddle演示:

I added: 我补充说:

$("html, body").animate({ scrollTop: 5000 }, 2000);

This is just a simple example. 这只是一个简单的例子。 Be sure to include the jQuery library. 确保包括jQuery库。 I'm sure there are a more elegant way. 我敢肯定还有一种更优雅的方式。 Please around a bit 请稍等一下

您可以在JavaScript中使用它

 window.scrollTo(300, 500);

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

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