不同元素的 jQuery 排队动画问题

发布于 2024-10-26 11:50:13 字数 2079 浏览 10 评论 0 原文

大家好,我在链接动画时遇到问题,我的情况是:

HTML

...
<div id="content" class="home-layout clearfix">
    <section class="home-related">
        ...
    </section>
    <section class="search">
        <div class="left">
            <div class="panel">
                ...
            </div>
        </div>
        <div class="right">
            <a class="someclass" href="#">CLICK</a>
        </div>
    </section>
</div>
...

JS

...
setupPanel : function() {
    var $container = $("#content"),
        $toHide = $("#content section.home-related"),
        $toShow = $("#content div.left"),
        $toShowContent = $('#content div.left div.panel'),
        widthOpen = '602px',
        positionOpen = '-600px',
        widthClose = '30px',
        positionClose = '0',
        durationSlide = 300,
        durationFade = 300;


    if(MyApp.vars.searchAperto == false){
        MyApp.vars.searchAperto = true;
        //ANIMATION ONE
        $toHide.fadeOut(durationFade, function(){
            $toShow.animate({
                width: widthOpen,
                left: positionOpen
                }, durationSlide, function(){
                    $toShowContent.fadeIn(durationFade);
            });
        });
    }else{
        MyApp.vars.searchAperto = false;
        //ANIMATION TWO
        $toShowContent.fadeOut(durationFade, function(){
            $toShow.animate({
                    width: widthClose,
                    left: positionClose
                }, durationSlide, function(){
                    $toHide.fadeIn(durationFade);
            });
        });
    }
},
...

一切正常。如果我点击“CLICK”,“ANIMATION ONE”就会正确执行;在动画结束时,如果我再次单击“单击”,“动画二”将正确执行;如果我在动画一正在进行时(或动画二正在进行时)单击“单击”,一切都会变得混乱......我想要的行为是,如果我在动画正在播放时单击“单击”继续,动画继续播放直到结束,之后,另一个动画开始播放,如果我再次单击相同的行为,依此类推...基本上我想要一个队列:动画一,动画二,动画一个等等...取决于我的点击。

我尝试使用应用于 $container 的 .queue() 和 .dequeue() 方法,但没有成功;我无法判断点击时我的应用程序应该将动画放入队列中而不是执行它...

请帮助我,我快疯了:D

Hi everybody i have a problem with chaining animations my situation is:

HTML

...
<div id="content" class="home-layout clearfix">
    <section class="home-related">
        ...
    </section>
    <section class="search">
        <div class="left">
            <div class="panel">
                ...
            </div>
        </div>
        <div class="right">
            <a class="someclass" href="#">CLICK</a>
        </div>
    </section>
</div>
...

JS

...
setupPanel : function() {
    var $container = $("#content"),
        $toHide = $("#content section.home-related"),
        $toShow = $("#content div.left"),
        $toShowContent = $('#content div.left div.panel'),
        widthOpen = '602px',
        positionOpen = '-600px',
        widthClose = '30px',
        positionClose = '0',
        durationSlide = 300,
        durationFade = 300;


    if(MyApp.vars.searchAperto == false){
        MyApp.vars.searchAperto = true;
        //ANIMATION ONE
        $toHide.fadeOut(durationFade, function(){
            $toShow.animate({
                width: widthOpen,
                left: positionOpen
                }, durationSlide, function(){
                    $toShowContent.fadeIn(durationFade);
            });
        });
    }else{
        MyApp.vars.searchAperto = false;
        //ANIMATION TWO
        $toShowContent.fadeOut(durationFade, function(){
            $toShow.animate({
                    width: widthClose,
                    left: positionClose
                }, durationSlide, function(){
                    $toHide.fadeIn(durationFade);
            });
        });
    }
},
...

Everything is working. If i click on the "CLICK", "ANIMATION ONE" is executed correctly; at the end of the animation if i click again on the "CLICK" "ANIMATION TWO" is executed correctly; if i click on the "CLICK" while animation one is going (or while animation two is going) everything mess up.... the behavior that i would like to have is that if i click on the "CLICK" while an animation is going, the animation continue to go until his end, and after, the other animation start to go and if i click again the same behavior and so on... basically i would like to have a queue: anim one, anim two, anim one, etc... depending from my clicks.

I tried using the .queue() and .dequeue() methods applied to $container but without success; i cannot tell that on the click my app should put the animation in the queue and not execute it...

please help me i'm getting crazy :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说好的呢 2024-11-02 11:50:13

我看到您采取了在 .queue() 的方法>$container 这实际上是解决这个问题的一个非常好的方法。希望您只是在理解 .queue() 时遇到问题 这段代码将为您工作:

$container.queue(function( next ) {
    // I moved this if to inside of the queue, this way
    // even if you click 3 times, it will hide / show / hide
    if(MyApp.vars.searchAperto == false){
        MyApp.vars.searchAperto = true;
        //ANIMATION ONE
        $toHide.fadeOut(durationFade, function(){
            $toShow.animate({
                width: widthOpen,
                left: positionOpen
                }, durationSlide, function(){
                    // note, the final callback on fadeIn will call the "next"
                    // function in the "queue"
                    $toShowContent.fadeIn(durationFade, next);
                });
            });    
        });
    }else{
        MyApp.vars.searchAperto = false;
        //ANIMATION TWO
        $toShowContent.fadeOut(durationFade, function(){
            $toShow.animate({
                width: widthClose,
                left: positionClose
            }, durationSlide, function(){
                // again call next from callback of last anim in the chain
                $toHide.fadeIn(durationFade, next);
            });
        });
    }
});

jQuery 中使用的默认队列将自动启动,因此您无需搞乱 .dequeue() 根本。队列函数的第一个参数将是 next - 该函数将使下一个队列函数出队,因此您可以使用 next() 将队列推进到它的位置。下一步。

I see you took an approach of using .queue() on $container which is actually a really good solution to this problem. Hopefully you were just having problems understanding .queue() and this code will work for you:

$container.queue(function( next ) {
    // I moved this if to inside of the queue, this way
    // even if you click 3 times, it will hide / show / hide
    if(MyApp.vars.searchAperto == false){
        MyApp.vars.searchAperto = true;
        //ANIMATION ONE
        $toHide.fadeOut(durationFade, function(){
            $toShow.animate({
                width: widthOpen,
                left: positionOpen
                }, durationSlide, function(){
                    // note, the final callback on fadeIn will call the "next"
                    // function in the "queue"
                    $toShowContent.fadeIn(durationFade, next);
                });
            });    
        });
    }else{
        MyApp.vars.searchAperto = false;
        //ANIMATION TWO
        $toShowContent.fadeOut(durationFade, function(){
            $toShow.animate({
                width: widthClose,
                left: positionClose
            }, durationSlide, function(){
                // again call next from callback of last anim in the chain
                $toHide.fadeIn(durationFade, next);
            });
        });
    }
});

The default queue used in jQuery will auto-start, so you don't need to mess with .dequeue() at all. The first argument to a queue function is going to be next - a function that will dequeue the next queue function, so you can use next() to advance the queue to its next step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文