如何为这个运动设置动画,以便稍后调用该函数时,该元素将设置动画?我通常使用 $(this).animate
但不确定如何将它包装在其中,因为我正在计算它的位置。谢谢。
jQuery.fn.center = function ()
{
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
$(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
$(window).scrollLeft()) + "px");
return this;
}
最佳答案
您可以像您提到的那样使用 this.animate
而不是使用 this.css
。
示例
this.animate({
top : Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
$(window).scrollTop()) + "px"
},800,function(){ });
Demo
关于javascript - 动画化div的居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15899181/