简体   繁体   English

jQuery-运行功能,即使之前的功能尚未触发。

[英]Jquery - Run function even when function before hasn't fired.

This is a snippet within a toggle() function. 这是toggle()函数中的代码段。 I have a header that when clicked, makes the headers below slide down creating space for the clicked headers content to fade in. 我有一个标题,单击该标题可使下面的标题向下滑动,从而为单击的标题内容提供淡入的空间。

There works fine but I am having a problem with the header at the bottom of the stack. 那里工作正常,但是我在堆栈底部的标题有问题。 There isn't a header below it to move down so the following function which fades the contents in, doesn't fire. 它下方没有标题可向下移动,因此不会触发以下淡入内容的功能。

I want to keep the general structure of the headers sliding down THEN the fade in function is fired. 我想保持标题的总体结构向下滑动,然后触发淡入功能。

$(currentHeader).next('li.header')
        .animate({ marginTop: itemHeight + 15 }, 1000, function ()
        {
            $(this).prev('ul#work-headers li').find('ul').fadeIn(500);
        });

Any help would be much appreciated. 任何帮助将非常感激。

I would say there are 2 solutions to this: 我会说有两种解决方案:

The quick and dirty: 快速而肮脏:

Add an empty LI-tag: 添加一个空的LI标签:

<li class="header" style="display:none;"></li>

Or change the code to code to add the margin to either the UL's or some div after each LI & UL pair. 或将代码更改为代码,以在每个LI&UL对之后为UL或某个div添加余量。

I can't really test this so you'll have to work out the kinks, but the basic idea is to encapsulate the logic you want, so that it can be invoked separately from the animation that doesn't fire in some cases. 我无法真正测试这一点,因此您必须解决一些问题,但是基本思想是封装所需的逻辑,以便可以将其与在某些情况下不会触发的动画分开调用。

var fadeIn= function(content) {
                    $(content).find('ul').fadeIn(500);
                }  
var next = $(currentHeader).next('li.header');

if(next.length>0) {
    next.animate({ marginTop: itemHeight + 15 }, 1000, function ()
        {
            fadeIn($(this).prev('ul#work-headers li'));
        });
}
else {
    fadeIn($(this).find('ul#work-headers li'));
}

edit - like @Holger's idea better to be honest 编辑-说实话就像@Holger的想法

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

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