在高度动画后获取回调
Get callback after height animation
这里我试图在高度动画之后运行一些代码
<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');}
});
我不能把我想在高度动画后运行的代码放在animate方法回调中,因为动画框脚本放在一个文档中,而我想在另一个文档运行的代码。
使用jquery.proise((.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');
});
})
});
固定Fiddle
事件驱动的JavaScript a la jQuery:
//file 1
$("#box").animate({height:"300px"},function() {
$(this).trigger('myBoxFinishedAnimatingHeight');
});
//file 2
$('#box').on('myBoxFinishedAnimatingHeight',function() {
//your code
});
您可以简单地将complete
属性用作回调函数:
.animate( properties [, duration ] [, easing ] [, complete ] )
发件人http://api.jquery.com/animate
这是一个演示
$("#adjest").animate({"height":"300px"},1000);
$("#adjest").promise().done(
function() {
alert("done");
}
);
如果你不能在animate((中运行它,我会使用promise和done。参见此处
http://jsfiddle.net/5p8Ww/
您可以为文件命名空间并调用函数。。。
var NameSpace = Namespace || {};
NameSpace.yourFunction() {
//Do stuff...
}
然后在您的html/other_file 中
.animate( properties , 1000, ease, NameSpace.yourFunction())
相关文章:
- 从新的WordPress媒体上传程序获取JS回调
- 如何在没有回调的情况下获取有关元素的信息
- 使用reactjs setState回调获取上一个输入值的计算值
- javascript/获取滚动条位置的回调
- Angular 2中Observable中获取数据后的队列/回调函数
- Yii ajaxbutton : 如何在成功回调函数中获取 $(this)
- 从异步回调中获取值
- DimpleJS-在getTooltipText回调中获取原始数据
- 使用回调获取图像像素数据
- 如何获取 jQuery 回调触发返回值
- 从 Spotify 应用程序中的回调获取专辑数据
- less.js - 在解析器的回调中获取变量值
- jQueryUI FullCalendar:从服务器获取事件后的回调
- 从回调函数获取xhr状态
- 无法获取函数的ajax回调
- 从回调中获取更详细的错误信息
- NodeJS异步模块:如何在回调中获取results对象
- 无法在pubnub javascript回调中获取错误
- 获取完整页面上加载的幻灯片.js slideload回调
- 如何获取 Ember 组件对象内部承诺成功或错误回调