javascript - 动画化div的居中

标签 javascript css animation center

如何为这个运动设置动画,以便稍后调用该函数时,该元素将设置动画?我通常使用 $(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/

相关文章:

javascript - 将类添加到动态添加的元素

javascript - 可滚动标签栏小部件解决方案

javascript - SharePoint jquery javascript 无法运行,但可以从控制台运行

animation - Unity Animator 默认图层状态在第一帧卡住

html - ng-hide 无法正常工作

php - 上传前检查文件内容

html - 链接 css 和 html 文件

css - 无法让列表项排队

css - 在触地和向上事件时更改多个元素类

javascript - 在JavaScript中计算跳跃动画