javascript - 获取高度动画后的回调

标签 javascript jquery

Here我尝试在高度动画之后运行一些代码

<button id="btn1">Animate height</button>
<div id="box"style="background:#98bf21;height:100px;width:100px;margin:6px;"></div>

$(document).ready(function () {
  $("#btn1").click(function () {
    $("#box").animate({height:"300px"});
  });

  var x = $('#box').height();
  if(x == 300){alert('animation is finished');}
});

我无法将我想要在高度动画之后运行的代码放入动画方法回调中,因为动画框脚本放置在一个文档中,而我想要在其他文档中运行的代码。

最佳答案

使用jquery .promise().done

$(document).ready(function () {
    $("#btn1").click(function () {
        $("#box").animate({
            height: "300px"
        }).promise().done(function () {
            alert('animation is finished');
        });;
    })
});

或者像这样分开:

$(document).ready(function () {
    $("#btn1").click(function () {
        $("#box").animate({
            height: "300px"
        });

        $("#box").promise().done(function () {
            alert('animation is finished');
        });
    })
});

Fixed Fiddle

关于javascript - 获取高度动画后的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25022878/

相关文章:

javascript - 如何在 Chrome 扩展程序中安全地存储密码?

PHP - 无法插入正确的时间格式

javascript - JQuery 中 HTML5 页面之间传递参数

单击 Jquery 目标 div 内

javascript - 如何防止下拉菜单在用户单击时关闭?

javascript - 在 v8 中声明具有未知值的(类)属性的最佳(性能最高)方法

javascript - jQuery: animate( 参数, [持续时间], [缓动], [回调] )

javascript - 使用 projekktor/jquery/javascript 停止加载/缓冲视频

javascript - 如何让所有带有类的文本框在 jquery 中有一个日期选择器?

javascript - 在 amazon.com 上插入 bootstrap popover