【问题标题】:Disable clickable div until function is executed.禁用可点击的 div 直到函数被执行。
【发布时间】:2015-10-24 06:05:57
【问题描述】:

我正在使用 Jquery 为动画序列制作动画菜单。我试图让它等到动画停止才能开始下一个。 因此,假设您单击一个按钮,它会开始显示动画元素,并且在功能中的所有内容完成之前您不能单击另一个按钮。所以我尝试在函数的开头和结尾使用 .css('pointer-events','none') 和 .css('pointer-events','auto') ,但它似乎不起作用。按钮仍然是可点击的。有没有其他方法可以防止在动画运行时点击可点击的 div 元素?

$(document).ready(function(){

            //menu buttons start animations
            $('#topstickpart').delay(300).show("slide", { direction: "up" }, 700);
            $('#topstickpart').effect( "bounce",{distance:6}, {times:3}, 600 );
            $('#bottomstickpart').delay(500).show("slide", { direction: "down" }, 700);
            $('#bottomstickpart').effect( "bounce",{distance:6}, {times:3}, 600 );
            $('#biobutton').delay(300).show("slide", { direction: "left" }, 700);
            $('#biobutton').effect( "bounce",{distance:6}, {times:3}, 600 );
            $('#contactbutton').delay(500).show("slide", { direction: "left" }, 700);
            $('#contactbutton').effect( "bounce",{distance:6}, {times:3}, 600 );
            $('#portfoliobutton').delay(200).show("slide", { direction: "left" }, 700);
            $('#resumebutton').delay(600).show("slide", { direction: "left" }, 700);
            $('#screeners').delay(20).show("slide", { direction: "up" }, 700);
            $('#biowindow').hide();
            $('#screeners').effect( "bounce",{distance:1}, {times:0}, 10 );     
$('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','none');
            $('.mainpanel').delay(50).animate({width:"660px"}, function(){
                $('.screencontainer').delay(10).animate({height:"236px"},function(){
                    $('#biowindow').fadeIn(function(){
                        $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','auto');
                    });
                });
            });




//onclick functionality
$('#biobutton').click(function(){
    $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','none');
    $('#biowindow, #resumewindow, #contactwindow, #dworkswindow').hide(function(){
        $('.mainpanel').animate({width:"100px"}, function(){
            $('.screencontainer').animate({height:"100px"}, 100, function(){
                $('#screeners').hide("slide", { direction: "up" }, 300, function(){
                    $('.topscreen').animate({top:"-105px"});
                    $('#screeners').show("slide", { direction: "up"}, 300, function(){                          
                            $('.mainpanel').animate({width:"660px", paddingTop:"300px"}, function(){
                                $('.screencontainer').delay(10).animate({height:"236px"}, 300, function(){
                                    $('#biowindow').fadeIn(function(){
                                        $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','auto');
                                    });
                                });                     
                            });


                    });
                });

            });     
        });


    });
    $('#toppanelslide').slideUp(300,'swing');

});




$('#resumebutton').click(function(){

    $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','none');
    $('#biowindow, #resumewindow, #contactwindow, #dworkswindow').hide(function(){
        $('.mainpanel').animate({width:"100px"}, function(){
            $('.screencontainer').animate({height:"100px"}, 100, function(){
                $('#screeners').hide("slide", { direction: "up" }, 300, function(){
                    $('.topscreen').animate({top:"-315px"});    
                    $('#screeners').show("slide", { direction: "up"}, 300, function(){                                              
                            $('.mainpanel').animate({width:"800px", paddingTop:"100px"}, function(){
                                $('.screencontainer').delay(10).animate({height:"720px"},function(){
                                    $('#resumewindow').fadeIn(function(){
                                        $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','auto');
                                    });
                                });                     
                            });


                    });
                });

            });     
        });


    });
    $('#toppanelslide').slideUp(300,'swing');

});



$('#portfoliobutton').click(function(){
    $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','none');
    $('#biowindow, #resumewindow, #contactwindow, #dworkswindow').hide(function(){
        $('.mainpanel').animate({width:"100px"}, function(){
            $('.screencontainer').animate({height:"100px"}, 100, function(){
                $('#screeners').hide("slide", { direction: "up" }, 300, function(){
                    $('.topscreen').animate({top:"-105px"});
                    $('#screeners').show("slide", { direction: "up"}, 300, function(){                          
                            $('.mainpanel').animate({width:"800px", paddingTop:"300px"}, function(){
                                $('.screencontainer').delay(10).animate({height:"340px"},function(){
                                    $('#dworkswindow').fadeIn(function(){
                                        $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','auto');
                                    });
                                });                     
                            });


                    });
                });

            });     
        });


    });
    $('#toppanelslide').slideDown(300,'swing');

});



$('#contactbutton').click(function(){
    $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','none');
    $('#biowindow, #resumewindow, #contactwindow, #dworkswindow').hide(function(){
        $('.mainpanel').animate({width:"100px"}, function(){
            $('.screencontainer').animate({height:"100px"}, 100, function(){
                $('#screeners').hide("slide", { direction: "up" }, 300, function(){
                    $('.topscreen').animate({top:"-105px"});
                    $('#screeners').show("slide", { direction: "up"}, 300, function(){                          
                            $('.mainpanel').animate({width:"660px", paddingTop:"300px"}, function(){
                                $('.screencontainer').delay(10).animate({height:"250px"},function(){
                                    $('#contactwindow').fadeIn(function(){
                                        $('#biobutton, #resumebutton, #resumebutton,#contactbutton').css('pointer-events','auto');
                                    });
                                });                     
                            });


                    });
                });

            });     
        });


    });
    $('#toppanelslide').slideUp(300,'swing');

});





});

【问题讨论】:

  • 可以在 Question 中包含html 吗,创建 stacksn-ps , jsfiddle jttp://jsfiddle.net 来演示?
  • 我建议阅读DRY
  • 我会非常关心这么多嵌套的“未完成”回调功能的性能。
  • 没有其他方法可以创建该动画,只能使用一长串 jquery 动画。我需要一个面板在单击的按钮上滑入,框架元素改变它们的比例和内部内容以淡入。所有这些都在单击另一个按钮时向后重复。一个问题是,一旦您在功能完成之前单击另一个按钮,一切都会变得疯狂。我需要重置所有点击动画或禁用可点击性直到完成。

标签: jquery html css


【解决方案1】:

尝试使用 .is():animated 选择器 、 .off()

var elems = $("div");
// only one `div` element at a time should be animated,
// on `click` event of any `div` element 
// if `this` is not currently animated , or another `div` currently animated
function fx() {
  console.log($(this).is(":animated"));
  // if `this` is not currently animated, do stuff
  // remove `click` event from `elems`,
  // call `.animate()` on `$(this)` jQuery object
  !$(this).is(":animated") && elems.off("click", fx) && $(this).animate({
    fontSize: "+=20"
  }, 2000, function() {
    // when animation complete re-attach `click` event to `elems`
    elems.click(fx)

  })
}

elems.click(fx)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>click 1</div>
<div>click 2</div>
<div>click 3</div>

【讨论】:

  • 我基本上需要在执行功能时禁用所有其他按钮。在您的示例中,您仍然可以在函数运行时单击所有这些。
  • @NebularDust "在执行函数时需要禁用所有其他按钮。" 查看更新后的帖子,将 click 处理程序定义为命名函数,添加了 .off()
  • 我会尽量绕开它,但它看起来是我需要的,谢谢。
【解决方案2】:

乍一看,我建议您阻止点击时的默认行为

jQuery('body').on('click', 'button.selector', function(e){ 
e.preventDefault();
})

完成后你可以做相反的事情

jQuery('body').on('click', 'button.selector', function(e){ 
return true;
})

干杯!

【讨论】:

  • 嗯,好像没用。我试着把它写成 $('body').on('click', '#biobutton, #resumebutton, #resumebutton,#contactbutton', function(e){ return true; });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 2015-01-02
  • 1970-01-01
  • 2013-08-05
相关资源
最近更新 更多