【发布时间】:2015-05-03 00:00:56
【问题描述】:
我有标签逻辑,可以在包装器中加载 html 模板。这很好用,但我包含了一个动画,当切换选项卡时,它会为选项卡包装器的 height 设置动画。
问题如下:当模板包含<img src="/some-image.png"> 时,$('#tab-content').load('template-url', function() {...}) 回调函数有时会在浏览器显示图像之前执行。而且我的动画不能正常工作。
var currentHeight = $contentHolder.height();
$contentHolder.load(path, function() {
$contentHolder.stop();
function animateHeight() {
var loadedContentHeight = $contentHolder.css('height', 'auto').height();
$contentHolder.height(currentHeight);
$contentHolder.animate({
height: loadedContentHeight
}, 800, 'linear');
}
animateHeight();
});
我尝试设置小超时,但并非每次都有效。如果我设置超过 300 毫秒的超时时间,感觉标签更改太慢了。
我尝试在$('img').load(function() {}) 被触发时执行动画,但没有成功。
当网页完全刷新并且第一次加载每个标签内容时,最常发生此错误。
【问题讨论】:
标签: javascript jquery animation asynchronous dom-events