Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery animate(),阻止动画_Jquery - Fatal编程技术网

Jquery animate(),阻止动画

Jquery animate(),阻止动画,jquery,Jquery,作为调用jQueryanimate函数的一部分,有时我想要阻止动画。作为动画制作功能的一部分,是否有办法防止动画制作?例如使用before属性 我想这样做: $('#myMovingDiv').animate( { before: // RUN A FUNCTION, IF FUNCTION RETURNS TRUE, CANCEL ANIMATION left: '+=100' } , 1000, function(){}); 为了解决下面的评论,我不能在动画之前只执行if,

作为调用
jQuery
animate
函数的一部分,有时我想要阻止动画。作为
动画制作
功能的一部分,是否有办法防止动画制作?例如使用
before
属性

我想这样做:

$('#myMovingDiv').animate(
{
    before: // RUN A FUNCTION, IF FUNCTION RETURNS TRUE, CANCEL ANIMATION
    left: '+=100'
}
, 1000, function(){});
为了解决下面的评论,我不能在动画之前只执行
if
,因为动画会更改
if
的值,如果动画未完成,则不满足
if
条件。 范例-

//THIS WILL NOT WORK IF ANIMATION IS NOT FINISHED BEFORE 2ND CLICK
$("#myClickableDiv").click(function () {
    if (document.getElementById(myMovingDiv').offsetLeft == 0) {

       $('#myMovingDiv').animate({
           left: '+=850'
       }, 400, function () {
        // Animation complete.
       });

    };
});

我认为这是不可能的,但您可以使用
.filter()

或者,为什么不在调用animate方法之前设置if条件呢

if(condition){
    $('#myMovingDiv').animate({
         left: '+=100'
     }, 1000, function () {});
}

我认为这是不可能的,但您可以使用
.filter()

或者,为什么不在调用animate方法之前设置if条件呢

if(condition){
    $('#myMovingDiv').animate({
         left: '+=100'
     }, 1000, function () {});
}

这取决于你到底想做什么。如果这是一个防止属性被动画化的问题,在本例中是
left
,那么您可以使用jQuery animate的

演示:

取决于你到底想做什么。如果这是一个防止属性被动画化的问题,在本例中是
left
,那么您可以使用jQuery animate的

演示:
我认为这是一个奇怪的重新任务,因为你可以在
之前执行
,但如果你绝对希望在
选项之前执行
,请将此脚本放在代码中的某个位置:

(function($){
    $.fn.oldAnim = $.fn.animate;
    $.fn.animate = function(){
        var args = arguments;
        if(typeof args[1] == 'object'){
            if(args[1].before){
                var callBefore = args[1].before().apply(this);
                if(callBefore || callBefore == undefined){
                    return this.oldAnim.apply(this, args);
                }else{
                    return this;
                }
            }
        }
        return this.oldAnim.apply(this, args);
    }
})(jQuery)
然后,您将有一个
before
选项。返回false、0、null或空字符串将取消动画。但是,返回未定义将触发动画

你可以这样称呼它:

$('selector').animate({'hegit' : 'value'}, {before : function(){}, complete : function(){} /*etc, etc*/})

小提琴:

我认为这是一个奇怪的重新任务,因为你可以只做一个
if
,但是如果你绝对想在
选项之前加一个
,把这个脚本放在你的代码中的某个地方:

(function($){
    $.fn.oldAnim = $.fn.animate;
    $.fn.animate = function(){
        var args = arguments;
        if(typeof args[1] == 'object'){
            if(args[1].before){
                var callBefore = args[1].before().apply(this);
                if(callBefore || callBefore == undefined){
                    return this.oldAnim.apply(this, args);
                }else{
                    return this;
                }
            }
        }
        return this.oldAnim.apply(this, args);
    }
})(jQuery)
然后,您将有一个
before
选项。返回false、0、null或空字符串将取消动画。但是,返回未定义将触发动画

你可以这样称呼它:

$('selector').animate({'hegit' : 'value'}, {before : function(){}, complete : function(){} /*etc, etc*/})

小提琴手:

谢谢阿伦,但这仍然有我在问题中提到的问题。(我补充了其他信息)。在动画完成之前多次单击,会导致此操作不起作用。谢谢Arun,但这仍然存在我在问题中提到的问题。(我补充了其他信息)。动画完成前多次单击会导致此操作不起作用。感谢您的回复Karl Andre,您的代码给了我一个错误-Object#没有“应用”方法感谢您的回复Karl Andre,您的代码给了我一个错误-Object#没有“应用”方法