【问题标题】:Stop and rollback other jQuery animate objects when mouseenter a new one当鼠标进入新对象时停止和回滚其他 jQuery 动画对象
【发布时间】:2012-11-09 11:34:25
【问题描述】:

mouseEnter 是一个图形时,我正在开发一个带有活动类.slider-active 的jQuery 滑块。

通过这种方式,我想以很酷的效果为我的.slider-imgcontainer 和我的 figcaption 制作动画:

.slideractive改变时,前一个对象必须将.slider-imgcontainer的宽度减小到250px,并且在figcaptionwidthpadding之后减小到0。当我减小widthfigcaption,文字太高了,所以我只用.hide.show来纠正这个问题。

然后滑块开始工作,但是......当鼠标滚过多个图形时,它们都被动画化了。我已尝试纠正此问题,但找不到任何解决方案(使用 .clearQueue() et .stop())。

结果:current slider

主要动画代码:

$(document).ready(function(){


    var GLOBAL = {
        window:$(window),
        slider:$('.slider-work'),
        container:$('#container'),
        figure:$("figure")
    }

    /********* SLIDER MAIN *************/
    // INIT LARGEUR ---
    GLOBAL.slider.width($(".slider-work img").size() * 250 + 900);

    // save width of figcaption in attr to animate them after (no auto animate)
    GLOBAL.slider.find("figcaption").each(function(i) {
        var t = $(this);
        if(!t.parent().hasClass("slider-active"))
            t.hide();

        t.attr("largeur", t.width());


    });


    // hover
    GLOBAL.slider.children("figure").mouseenter( function () {


        var oldActive = $(".slider-active");
        var thiss = $(this);

        oldActive.removeClass("slider-active");
        thiss.addClass("slider-active");

        oldActive.find("figcaption").animate({
            width: 0,
            padding: 0
            }, 220, 'linear', function() {
                oldActive.find(".slider-imgcontainer").animate({
                    width : 250
                    }, 400, 'linear', function() {

                            // Animation de l'ancien active fini , alors : 

                            //$(".slider-imgcontainer").removeAttr("style");


                            thiss.removeAttr("style").children(".slider-imgcontainer").animate({
                                    width : 400
                                }, 400, 'linear', function(){
                                    var largeurFigcaption = thiss.find("figcaption").show().attr("largeur");

                                    thiss.find("figcaption").animate({
                                        width : largeurFigcaption,
                                        padding : " 0 20px 20px 20px"
                                        }, 220, 'linear', function(){


                                    });
                            });


                    });
        });
    });
    // END MouseEnter


//End ready
});

还有我调试滑块的代码

 GLOBAL.slider.children("figure").mouseout( function () {
            var thiss = $(this);
            //$("#title span").append(" toto");


            var myChildrenBehave =  GLOBAL.slider.filter(function() {
                var filtered = $(this).children().is(":animated");
                return filtered;
            });

            myChildrenBehave.clearQueue().stop();



        });

我接受所有想法:)

【问题讨论】:

    标签: jquery jquery-animate mouseenter mouseleave animated


    【解决方案1】:

    有多种方法可以解决这个问题,但我更喜欢的一种是在初始鼠标悬停动画完成后检查鼠标是否仍在该项目上。如果不是,则将其关闭。

    jQuery 没有内置的检查鼠标是否仍在我知道的对象上,但我为非常简单的情况编写了自己的一个。

    var mouse = {
    mouseX: null,
    mouseY: null,
    mouseInWindow: true,
    init: function() {
        $(document).bind('mousemove', function(event) {
            mouse.mouseX = event.pageX;
            mouse.mouseY = event.pageY;
            mouse.mouseInWindow = true;
        });
        $(document).bind("mouseleave", function(event) {
            mouse.mouseInWindow = false;
        });
        $(document).bind("mouseenter", function(event) {
            mouse.mouseInWindow = true;
        });
    },
    isOver: function($element) {
        $elementPosition = $($element).offset();
        $elementWidth = $($element).width();
        $elementHeight = $($element).height();
        $returnValue = true;
        if (mouse.mouseX !== null) {
            if (mouse.mouseX < $elementPosition.left) { $returnValue = false; }
            if (mouse.mouseY < $elementPosition.top) { $returnValue = false; }
            if (mouse.mouseX > $elementPosition.left + $elementWidth) { $returnValue = false; }
            if (mouse.mouseY > $elementPosition.top + $elementHeight) { $returnValue = false; }
            if (!mouse.mouseInWindow) { $returnValue = false; }
        }
        return $returnValue;
    }
    

    }

    您需要在其他代码之前运行 mouse.init(),然后您可以使用 mouse.isOver($(yourSlideActivator));

    【讨论】:

    • 干得好,它正在工作! :) 我将您的代码与 animate 的 step 选项一起使用:step: function( now, fx ){ if(!mouse.isOver(thiss)) $(this).stop(); }, 谢谢,现在我只需要优化代码以加快滑动速度!
    • 您应该尝试一次,让它完成动画,而不是在中间停止。这通常可以带来令人愉悦的级联效果,避免出现断断续续的停止。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2012-05-04
    • 2012-07-22
    • 1970-01-01
    • 2011-03-09
    相关资源
    最近更新 更多