简体   繁体   English

滚动到手风琴顶部

[英]Scrolling to top inside an accordion

In my page I have 2 scrolls. 在我的页面中,我有2个卷轴。

One is apparent in the page ( They are not loaded with ajax or something.) 页面中明显有一个(它们未加载ajax或其他内容。)

The other one is inside an accordion. 另一个在手风琴内。

I want these scroll to be always at the top. 我希望这些滚动条始终位于顶部。 They have same class names. 它们具有相同的类名。

With the snipets I can achieve my first goal, scrolling to top in apparent scroll 使用摘录,我可以实现我的第一个目标,以明显的滚动方式滚动到顶部

$('.m-messenger__messages').scrollTop($('.m-messenger__messages')[0].scrollHeight);

As well as with this one 以及与此

var messageBody = document.querySelector('.m-messenger__messages');
messageBody.scrollTop = messageBody.scrollHeight - messageBody.clientHeight;

However, the scroll inside the accordion menu is not affected by this change. 但是,手风琴菜单内的滚动不受此更改的影响。

If I open the accordion and run this snippets it scrolls to top. 如果我打开手风琴并运行此代码段,则会滚动到顶部。 So that either I need to find a way to run this snippet not only to apperent but also all scrolls in the page or when I click the accordion this javascript code needs to be executed. 因此,我不仅需要找到一种方法来运行此代码段,不仅可以使它们正常运行,而且还可以运行页面中的所有滚动,或者当我单击手风琴时,需要执行此javascript代码。

I would like to solve this problem with the first solution. 我想用第一个解决方案解决这个问题。

I tried this and I couldn't succeed as well. 我尝试过,但也无法成功。 If I put alert() rather than scrolltop inside this function, I got the alertbox. 如果我将alert()而不是scrolltop放在此函数中,则会收到警报框。

$(".m-accordion__item").click(function() {
    $('.m-messenger__messages').scrollTop($('.m-messenger__messages')[0].scrollHeight);
});

How can I achieve this goal? 我怎样才能实现这个目标?

This solved my problem. 这解决了我的问题。 BTW this is bootstrap4. 顺便说一句,这是bootstrap4。 So that it may apply to any of bootstrap 4 templates. 因此它可以应用于任何bootstrap 4模板。

$('.collapse').on('shown.bs.collapse', function(e) {

$('.m-messenger__messages').scrollTop($('.m-messenger__messages')[0].scrollHeight);
});

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

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