简体   繁体   English

jQuery为一个div设置动画,完成后,为第一个div中的第二个div动画

[英]jquery animate a div and when is finish, animate the second div inside the first

i have for example a header (140px height) on the web with 5 "li" content. 例如,我在网上有5个“ li”内容的标头(高度为140px)。 The first 3 of this (A,B,C) open a subheader (50px height) when ".mouseover", the others 2 (D,E) close all to the begining. 当“ .mouseover”时,前三个(A,B,C)打开一个子标题(50px高度),其他两个(D,E)则关闭所有开头。

jq(document).ready(function(){
        jq("#A a,#B a,#C a").mouseover(function(){
            jq("#subheader").animate({top:"140px"},"normal");
        });

        jq("#D a,#E a").mouseover(function(){
            jq("#subheader").animate({top:"91px"},"normal");
        });
    });

Until here all is ok, now comes the problem. 到这里一切都OK,现在问题来了。 Depend of which A, B or C is hovered has to do diferent things with the subheader, just at the end of the animation. 取决于在哪个A,B或C上悬停,就必须在动画结束时对子标题进行不同的处理。 (open .smenus inside subheader) (在子标题内打开.smenus)

        jq("#A a").mouseover(function(){
            jq(".smenu1").slideDown("slow");
            jq(".smenu1 ul").animate({margin:"0px auto"},"slow");
            jq(".smenu1 li").animate({padding:"0 30px 0"},"slow");

        });
        jq("#B a").mouseover(function(){
            jq(".smenu2").slideDown("slow");
            jq(".smenu2 ul").animate({margin:"0px auto"},"slow");
            jq(".smenu2 li").animate({padding:"0 30px 0"},"slow");

        });
        jq("#C a").mouseover(function(){
            jq(".smenu3").slideDown("slow");
            jq(".smenu3 ul").animate({margin:"0px auto"},"slow");
            jq(".smenu3 li").animate({padding:"0 30px 0"},"slow");

        });

This doesn't work i think because it start loading before finishing the opening of the suheader. 我认为这行不通,因为它在完成suheader的打开之前就开始加载。 so i have to do this animation when the opening is finished. 所以当开幕结束时我必须做这个动画。

PD: And now another thing, when for example #A is hovered, and then #B, it has to stop the animation (if is not finished, or just callback), remove others submenus (in this case .smenu1) and replace it whith theirs submenu (in this case .smenu2). PD:还有另一件事,例如当#A悬停在#B上时,它必须停止动画(如果尚未完成,或者只是回调),删除其他子菜单(在这种情况下为.smenu1)并替换它和他们的子菜单(在本例中为.smenu2)。 #D and #E has to do the same, putting everything back to the begining. #D和#E必须做同样的事情,将一切重新开始。

EDITED: 编辑:

thanks to Simon, I have more or less this: http://jsfiddle.net/PAXqB/4/ 多亏了西蒙(Simon),我对此有所了解: http : //jsfiddle.net/PAXqB/4/

the last implementation is to make it with .click() and not with .mouseover() having this 3 cases: 最后一种实现是使用.click()而不是.mouseover()来实现,这有以下3种情况:

  • case 1: Same than mouseover, all opened. 情况1:与鼠标悬停相同,全部打开。
  • case 2: Click again the same Main, all have to close like my example. 情况2:再次单击同一Main,所有操作都必须像我的示例一样关闭。
  • case 3: Click on an other Main, close submenu, not suheader, and open the new submenu. 情况3:单击另一个主菜单,关闭子菜单,而不是suheader,然后打开新的子菜单。

You have a couple of options 您有几种选择

.animate() has a complete callback option : .animate()具有完整的回调选项:

jq(".smenu2 ul").animate({margin:"0px auto"},"slow", function() {
   // code here is executed after this animation is complete
});

Use .queue() , something like : 使用.queue() ,类似于:

jq(".smenu2").slideDown("slow");     
jq(".smenu2").queue(function () {
  jq(".smenu2 ul").animate({margin:"0px auto"},"slow");
  jq(this).dequeue();
});
jq(".smenu2 li").animate({padding:"0 30px 0"},"slow");

this simple queues up your animation requests and executes in order 这个简单的队列动画请求并按顺序执行

First of all, I'd recommend tidying up your code for readability and making it easier to work with. 首先,我建议您整理代码以提高可读性,并使其更易于使用。 Example: 例:

$("#A a, #B a, #C a").on('mouseover', function(){
            $menu = $(this).find(".menu");
            $menu.slideDown("slow");
            $menu.find("ul").animate({margin:"0px auto"},"slow");
            $menu.find("li").animate({padding:"0 30px 0"},"slow");
        }).on('mouseout', function(){
            $(this).find(".menu").stop(true, true);
});

Not fully tested, but this should be on the right lines in terms of getting your code working - the .stop() method being the second part of your answer. 尚未经过全面测试,但是就使代码正常工作而言,这应该是正确的做法-.stop()方法是答案的第二部分。

I must say I had to read your question multiple times and I'm not yet sure if I understood you right, but is this what you wanted to achieve? 我必须说我不得不多次阅读您的问题,但我不确定我是否理解正确,但这就是您想要实现的目标吗? http://jsfiddle.net/PAXqB/ http://jsfiddle.net/PAXqB/

$(document).ready( function() {
    $('ul.main > li').bind('mouseover mouseout', function(e) {
        var t = $(this), i = t.index(), sub = t.find('.sub');
        if(i < 3)
            switch(e.type) {
                case 'mouseover':
                    sub.stop(true).slideDown( function() {
                        $(this)
                            .stop(true)
                            .animate({ margin:"20px 0 0" })
                            .find('li')
                            .stop(true)
                            .animate({ padding:"0 10px 0" });
                    });
                    break;
                case 'mouseout':
                    sub.stop(true).slideUp('fast', function() {
                        $(this)
                            .removeAttr('style')
                            .find('li')
                            .removeAttr('style');
                    });
                    break;
            }
    });
});

I slightly adjusted the animate-to style so it looks a bit smoother on my jsfiddle, but I hope it helps you. 我对animate-to样式进行了些微调整,以使其在我的jsfiddle上看起来更平滑,但我希望它对您有所帮助。

Sooo this would be my best solution so far for a tablet: jsfiddle I'm working on a version where it acts exactly as you wanted (container just slides down/up if nothing was visible or the clicked item was active), but have some trouble with the animations atm. 所以,到目前为止,这将是我迄今为止在平板电脑上最好的解决方案: jsfiddle我正在开发一个版本,该版本的功能完全符合您的要求(如果看不见任何东西或单击的项目处于活动状态,则容器会向下/向上滑动),但是有一些动画atm遇到麻烦。

$(document).ready( function() {

    var active = 'active';

    $('ul.main > li').bind('click', function() {

        var t = $(this),
            i = t.index(),
            c = $('.sub-container'),
            sub = $( $('.sub').get(i) ),
            isActive = t.hasClass(active),
            li = sub.find('li'),
            liCount = li.length;

        c.slideUp('fast', function() {
            c.find('*').removeAttr('style');
            t.siblings().removeClass(active);
            if(i < 3) {
                if(isActive) t.removeClass(active);
                else {
                    t.addClass(active);
                    c.slideDown( function() {
                        sub.slideDown( function() {
                            li.animate({
                                width: (100 / liCount) + '%'
                            });
                        });
                    });
                }
            }
        });

    });

});

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

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