获取作为动画方法中属性参数传递的对象



对于在jquery对象中包装的dom元素,我需要获得当前位置,或者如果它当前正在动画,我需要获得其结束位置。是否有任何方法访问对象传递作为jquery.animate()方法中的属性参数?或者我是否需要将这些对象存储在某个地方,以便以后可以查找给定dom元素的动画方向?

"animate"方法的标准参数之一是"complete"回调。

http://api.jquery.com/animate/

在"complete"函数中,你可以通过在选择器中包装"this"来获取当前元素,如下所示:

$("div").animate({
    "width": '500'
}, 3000, function() {
    $(this).css({"background-color":"black"});
});
http://jsfiddle.net/Ana5R/

还有一个"进度"函数,根据您的需要在每一步调用。

我想到了一个办法:

例如,如果我想让一个对象向右移动50px或者它的运动方向我可以这样做:

var currentX=myJqueryObject.css("left");//Get the position it's currently at
myJqueryObject.finish();//finish the animation so its position instantly gets set to where its animating towards
var endx=parseInt(myJqueryObject.css("left"));//get its position now. If it wasn't animating when finish() was called then the call didn't change anything.
myJqueryObject.css("left",currentX);//set its position back to where it was before calling finish()
myJqueryObject.animate({left:endx+50});//now animate it 50px to the right of its current position or 50px to the right of the position it was animating towards

最新更新