获取在animate-method中作为properties-argument传递的对象

时间:2013-05-31 13:43:31

标签: jquery

对于包含在jquery-object中的dom元素,我需要获取当前位置或者如果它当前正在设置动画,我需要改为获得它的结束位置。有没有办法访问对象传递作为jquery.animate() - 方法中的properties-argument?或者我是否需要将这些对象存储在某个地方,以便我以后可以查看给定dom-element动画的位置?

2 个答案:

答案 0 :(得分:0)

“animate”方法的标准参数之一是“完整”回调。

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

在“完整”函数内部,您可以通过将“this”包装在选择器中来获取当前元素:

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

http://jsfiddle.net/Ana5R/

每个步骤都会调用“进度”功能,具体取决于您的需求。

答案 1 :(得分:0)

找出一种方法:

例如,如果我想要将对象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