简体   繁体   English

第三方滑倒动画后如何执行代码?

[英]How to execute code after 3rd party slide down animation?

Background: I am using the materialize JS to create collapsible divs. 背景:我正在使用物化 JS创建可折叠的div。 When a div that has a class of collapsible-header is clicked, a slide down animation occurs on the sibling div that has a class of collapsible-body, showing the contents of the collapsible-body. 单击具有可折叠标头类的div时,在具有一类可折叠体的同级div上会发生向下滑动动画,显示可折叠体的内容。

What I am trying to accomplish is when a collapsible header is clicked, the browser should scroll to the top of that div so that the contents are in full view of the user. 我要完成的工作是,单击可折叠的标题时,浏览器应滚动到该div的顶部,以便用户可以看到所有内容。 I have used an on click event, which works fine on the first collapsible. 我使用了on click事件,该事件在第一个可折叠对象上效果很好。 But then, if you have an open collapsible and you click on another to open it, it doesn't scroll to the top of the clicked div properly (it will scroll to the middle of the body, or way above the top of the div). 但是,如果您有一个打开的可折叠鼠标,然后单击另一个打开它,它将无法正确滚动到所单击的div的顶部(它将滚动到主体的中间,或者位于div顶部的上方)。

JS Fiddle of on click functionality: https://jsfiddle.net/f83dct8f/4/ 点击功能的JS Fiddle: https//jsfiddle.net/f83dct8f/4/

I am able to accomplish what I am looking to do by editing the Materialize JS itself. 通过编辑Materialize JS本身,我可以完成我想做的事情。 Here is how the line looks before my edit: 这是我编辑之前的样子:

 object.siblings('.collapsible-body').stop(true, false).slideDown({ duration: 350, easing: "easeOutQuart", queue: false, complete: function() { $(this).css('height', ''); } }); 

I add the following to the complete portion of the above statement to accomplish what I'm looking for: 我将以下内容添加到上述语句的完整部分中,以完成所需的操作:

$('body').animate({scrollTop: $('.collapsible-header.active').offset().top },'slow');

Question: I do not want to edit the Materialize 3rd party code to accomplish what I need, so I need to find a way to implement the body animate code after the slide down is finished, without editing the Materialize animation. 问题:我不想编辑Materialize 3rd Party代码来完成所需的操作,因此,我需要找到一种方法,在向下滑动完成后无需编辑Materialize动画即可实现人体动画代码。 I know there has to be a way to do it? 我知道必须有一种方法吗?

I have tried event queues on the collapsible body, which seemed promising, but my test console print executed during the animation. 我在可折叠主体上尝试了事件队列,这看起来很有希望,但是我的测试控制台打印在动画过程中执行。 See below. 见下文。 If someone could point me in the right direction of what else I could try, I would greatly appreciate it. 如果有人能指出我正确的方向,我将不胜感激。

$('.collapsible-header.active')
.siblings('.collapsible-body').slideDown().queue(function(){    

console.log('DONE');

});

You should be able to use the onOpen option when initialising your .collapsible . 初始化.collapsible时,您应该可以使用onOpen选项。 Pass in a function to execute when the accordion is opened. 传递一个函数以在打开手风琴时执行。 The function receives the element as a argument. 该函数接收元素作为参数。

$(document).ready(function() {
    $('.collapsible').collapsible({
        onOpen: function(el) {
            $('body').animate({scrollTop: el.offset().top },'slow');
        }
    });
});

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

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