javascript jQuery slimScroll 始终从底部开始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23088883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:18:52  来源:igfitidea点击:

jQuery slimScroll to always start at the bottom

javascriptjqueryhtmlcssslimscroll

提问by meetmahpuppy

I have a container that have already a slimscroll, and a button to add new item to the last element. I want the scrollBar of slimscroll to always start at the bottom. Any help please? :(

我有一个已经有一个 slimscroll 的容器,以及一个将新项目添加到最后一个元素的按钮。我希望 slimscroll 的滚动条始终从底部开始。请问有什么帮助吗?:(

<div>
  <ul id="item-container">
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
    <li>item 4</li>
    <li>item 5</li>
  </ul>
</div>

<input type="text" placeholder="add another item" id="add-input"/>
<button id="add-btn">Add</button>

Need help badly :(

非常需要帮助:(

http://jsfiddle.net/qh8ye/1/

http://jsfiddle.net/qh8ye/1/

回答by meetmahpuppy

I solved the problem, it seems I just have to add this code in the bottom every time I add the new item. Cheers! thanks! @gulty

我解决了这个问题,看来我每次添加新项目时都必须在底部添加此代码。干杯! 谢谢!@gulty

var scrollTo_int = itemContainer.prop('scrollHeight') + 'px';
    itemContainer.slimScroll({
    scrollTo : scrollTo_int,
    height: '200px',
    start: 'bottom',
    alwaysVisible: true
});

http://jsfiddle.net/qh8ye/3/

http://jsfiddle.net/qh8ye/3/

回答by Admir Heric

This was enough for me:

这对我来说已经足够了:

$('#messagelist').slimScroll({
        start: 'bottom'
});

Thanks meetmahpuppy!

感谢 meetmahuppy!

回答by Adeel Ilyas

Agreed with Admir, as per slimscroll documentationstartoption defined as

同意 Admir,根据slimscroll 文档开始选项定义为

start- top or bottom or $(selector) - defines initial position of the scrollbar. When set to bottom it automatically scrolls to the bottom of the scrollable container.

开始- 顶部或底部或 $(selector) - 定义滚动条的初始位置。当设置为底部时,它会自动滚动到可滚动容器的底部。

This will work only at Slimscroll initialization. If you want to adjust Slimscroll position after initialization then you can use scrollTooption.

这仅适用于 Slimscroll 初始化。如果你想在初始化后调整 Slimscroll 的位置,那么你可以使用scrollTo选项。

var container = $(selector);

container.slimScroll({
  scrollTo: container[0].scrollHeight
});

Every time you change Slimscroll position, it will show scroll bar for a while and then fade it out which will obviously make it distracting. For this, you can use this alternative

每次更改 Slimscroll 位置时,它都会显示滚动条一段时间然后淡出,这显然会让人分心。为此,您可以使用此替代方法

var container = $(selector);

container.scrollTop(container[0].scrollHeight);