【问题标题】:How can I make sure .animate has finished before performing again如何确保 .animate 在再次执行之前完成
【发布时间】:2012-08-04 23:55:05
【问题描述】:

我有一些使用 .animate 移动的滑动图像。当我单击箭头再次前进时,幻灯片都搞砸了。

这里是正在发生的事情的链接。 http://leadgenixhosting.com/~intmed/

这是我目前拥有的js:

$(document).ready(
function(){

    $('#mast').hover(
        function(){
            $('#out-right').hide().attr('src','');
            $('#in-right').show().attr('src','http://leadgenixhosting.com/~intmed/wp-content/themes/imi/images/on_03.gif');
            $('#out-left').hide().attr('src','');
            $('#in-left').show().attr('src','http://leadgenixhosting.com/~intmed/wp-content/themes/imi/images/on_07.gif');
        },
        function(){
            $('#in-right').hide().attr('src','');
            $('#out-right').show().attr('src','http://leadgenixhosting.com/~intmed/wp-content/themes/imi/images/off_03.gif');
            $('#in-left').hide().attr('src','');
            $('#out-left').show().attr('src','http://leadgenixhosting.com/~intmed/wp-content/themes/imi/images/off_07.gif');
        }
    );

    /*Picture Setup
    -------------------------------------------------*/

    var gallery_images = [];
    var divs = ['one','two','three','four'];

    $('#image_container img').each(
        function(){
            gallery_images[gallery_images.length] = $(this).attr('src');
            $(this).hide();
        }
    );

    $('#'+divs[0]+'').css('background-image','url('+gallery_images[0]+')');

    var total_images = gallery_images.length;

    //Images

    var current = 0;

    //Divs

    var current_image = 0;
    var previous_image = divs.length - 1;
    var two_prev = divs.length - 2;
    var next = current_image + 1;

    /*Image Switch
    -------------------------------------------------*/

    function imageSwitch(){

        current++;
        current_image++;
        previous_image++;
        next++;
        two_prev++

        if(two_prev >= divs.length){
            two_prev = 0;   
        }

        if(current >= gallery_images.length){
            current = 0;    
        }

        if(previous_image >= divs.length){
            previous_image = 0; 
        }

        if(current_image >= divs.length){
            current_image = 0;
        }

        if(next >= divs.length){
            next = 0;
        }   

        $('#'+divs[current_image]+'').animate({left:'-=1020px'},{queue:false,duration:1000})
        $('#'+divs[current_image]+'').css('background-image','url('+gallery_images[current]+')');

        $('#'+divs[previous_image]+'').animate({left:'-=1020px'},{queue:false,duration:1000});

        $('#'+divs[next]+'').animate({left: '+=1020px', top: '-=10000px'}, 1000);

        $('#'+divs[two_prev]+'').animate({left: '+=1020px', top: '+=10000px'}, 1000);
    }

    function imageBack(){

        current--;
        current_image--;
        previous_image--;
        next--;
        two_prev--;

        if(two_prev < 0){
            two_prev = divs.length - 1; 
        }

        if(current < 0){
            current = divs.length - 1;  
        }

        if(previous_image < 0){
            previous_image = divs.length - 1;   
        }

        if(current_image < 0){
            current_image = divs.length - 1;
        }

        if(next < 0){
            next = divs.length - 1;
        }   

        $('#'+divs[current_image]+'').animate({left:'+=1020px'},{queue:false,duration:1000});
        $('#'+divs[current_image]+'').css('background-image','url('+gallery_images[current]+')');

        $('#'+divs[previous_image]+'').animate({left:'-=1020px'},{queue:false,duration:1000});

        $('#'+divs[next]+'').animate({left: '-=1020px', top: '+=10000px'}, 1000);

        $('#'+divs[two_prev]+'').animate({left:'-=1020px'},{queue:false,duration:1000});
    }

    /*Image Buttons
    ----------------------------------------*/

    $('#in-right').click(
        function(){
            imageSwitch();
        }
    );

    $('#in-left').click(
        function(){
            imageBack();
        }
    );
}
);

【问题讨论】:

    标签: javascript jquery image jquery-animate


    【解决方案1】:

    API:.stophttp://api.jquery.com/stop/

    在匹配的元素上停止当前正在运行的动画。

    就像@ahren 提到的,如果您热衷于阅读这些内容,可以使用简单的条件检查is(":animated")

    http://api.jquery.com/animated-selector/

    你会在这里找到实现:jquery is(":visible") and is(":animated") bug during animation?

    【讨论】:

    • +1 比我的回答好!可能还值得一提.is(":animated")
    • @ahren SU​​p 兄弟!谢谢! :) 哦,当然 +1 给你,这件好事会回报给你 :)
    • @Tats_innit 感谢您的帮助!这很有帮助。
    • @ahren 也谢谢你。很有帮助
    【解决方案2】:
    $('#element').animate({..properties..}, time_interval, function() {
        // When here, you know that animate has finished
    });
    

    【讨论】:

    • 在这种情况下,回调是不合适的。 -1.
    • 为什么?您可以在动画时分配一个布尔值,并在动画完成后将其设为假。如果布尔值为真,则您不会为其他事物设置动画。当回调被调用时,你可以做其他动画完成后应该做的事情。
    • 问题标题具有误导性......如果用户再次点击,OP 实际上想要中止当前动画并继续下一个动画
    • 对于如此琐碎的事情来说代码太多了。一个简单的.stop() 就足够了,或者检查.is(':animated') - 同意nbrooks,标题具有误导性! (其实我是先有回拨作为我的答案,但在重新阅读问题后将其删除)
    • @nbrooks 是的,我不明白他想要什么 :)
    猜你喜欢
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多