2010-07-19 137 views
2

我在我的网页下面的动画:

 $(".anim-item").not(this).animate({ 
     opacity: 0, 
    }, { queue: true, duration: 1000 } , function() { 
     // Animation complete. 
    }); 

    $(this).animate({ 
     left: 200, 
    }, { queue: true, duration: 1000 } , function() { 
     // Animation complete. 
    }); 

目前,无论是动画同时运行。我希望第二个动画在第一个动画之后运行。我试图把第二个函数放在回调函数中,但是找不到获得$(this)引用的方法。任何想法如何得到这个工作?

在此先感谢。

回答

3

你的功能是错误,如果你正在申报选项,然后回调云在选择对象:

$(".anim-item").animate({ 
    opacity: 1, 
}, {duration: 1000, queue: true, complete: function() { 
    $(this).animate({ 
     left: 200, 
    }, { queue: true, duration: 1000, complete: function() { 
     // Animation complete. 
    }}); 
}}); 

另外,不要让包含该项目的全局变量,这只是自寻烦恼,尤其是jQuery将保持它为您在这个研究所ance,如果你需要声明一个链接对象的新变量,通常你做得不对;)

1

将它保存在一个不同的名称,如:

var myThis = this; 
$(".anim-item").not(this).animate({ 
    opacity: 0, 
}, { queue: true, duration: 1000 } , function() { 
    $(myThis).animate({ 
     left: 200, 
    }, { queue: true, duration: 1000 } , function() { 
     // Animation complete. 
    }); 
}); 

内部功能的关闭将确保它是可见的。

0

弥补这方面的别名通过

var _this = this; 

如果你写这样clickhover等一个jQuery查询$('.abc')和使用功能,this总是参照当前DOM节点的jQuery正在处理中。

0

将其存储在局部变量中。

var _this = this; 

$(".anim-item").not(this).animate({ 
     opacity: 0, 
    }, { queue: true, duration: 1000 } , function() { 
     // Animation complete. Next animation 
     $(_this).animate({ 
      left: 200, 
     }, { queue: true, duration: 1000 } , function() { 
      // Animation complete. 
     }); 
    } 
); 
1

两种方式:

  • 缓存this在一个局部变量调用.animate()
  • 使用.proxy()之前,您this引用传递到.animate()

例子1:

var func = function(){ 
    var self = this; 

    $(".anim-item").not(this).animate({ 
    opacity: 0, 
    }, { queue: true, duration: 1000 } , function() { 
     self.animate({}); 
    }); 
}; 

例2:

var func = function(){ 
    $.proxy($(".anim-item").not(this).animate({ 
    }), this); 
}; 
0

在一个jQuery回调函数this总是被设置为,所述函数适用于DOM元素。

如果你想在你的第一个回调函数访问this你必须创建一个对它的引用您之前的动画:

var self = this; 
$(".anim-item").not(this).animate({ 
    opacity: 0, 
}, { queue: true, duration: 1000 } , function() { 
    $(self).animate({ 
     left: 200, 
    }, { queue: true, duration: 1000 } , function() { 
     // Animation complete. 
    }); 
});