gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:06:28 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com