简体   繁体   English

如何在div的底部保持滚动条?

[英]How keep scroll bar in bottom in div?

I have many tabs and when click any tab, a vertical scrolling div is shown. 我有很多选项卡,单击任何选项卡时,都会显示一个垂直滚动div。 Like facebook messages page: When click to any User's name. 像Facebook消息页面一样:单击任何用户名时。 Dialog seems and scroll bar is on the bottom of div by default . 出现对话框,默认情况下滚动条位于div的底部

Here is tabs: 以下是标签:

<ul class="companies">
    @foreach (var item in Model)
    {
       <li id='company_@(item.Id)' data-id='@item.Id' >
           <a>@item.Name</a>
       </li>
    }
</ul>

And content: 内容:

@foreach (var item in Model)
{
   <div id="scrollingDiv"  class="scrollingDiv"> @item.News </div>
}

And CSS: 和CSS:

.scrollingDiv {
    overflow-x: hidden;
    max-height: 500px;
    overflow-y: scroll;
}

I want to keep scroll bar on the bottom when click any tab. 单击任何选项卡时,我想将滚动条保持在底部。 (To see always last news). (以查看总是最新消息)。 Also current div. 也目前股利。 I checked this questions. 我检查了这个问题。 But not worked in my application. 但不适用于我的应用程序。 How can I solve this issue? 我该如何解决这个问题?

With JavaScript, you can do something like 使用JavaScript,您可以执行以下操作

var d = document.getElementById('myDiv');

if(d.scrollHeight > d.clientHeight) {
  d.scrollTop = d.scrollHeight - d.clientHeight;
}

and execute this logic every time you add content to the div or when you want the scrollable container to scroll to the bottom. 并在每次将内容添加到div或希望可滚动容器滚动到底部时执行此逻辑。

jQuery's scrollTop() function might be just what you need. jQuery的scrollTop()函数可能正是您所需要的。 See http://api.jquery.com/scrollTop/ 请参阅http://api.jquery.com/scrollTop/

您可以使用以下代码来实现

$('.full-area').scrollTop($('.full-area')[0].scrollHeight);

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

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