【发布时间】:2017-05-21 09:37:00
【问题描述】:
编辑:所以现在它不是随机的,看起来它总是无法从 .css() 方法执行(没有进行任何更改)。仍然不明白我可能犯的错误。
我正在尝试使用 jQuery 和 animate.css 对 div 的删除进行动画处理。
问题是这个动画所依赖的事件和操作是随机执行的。
此代码在 .on("click"... 处理程序内响应 click 运行:
$('section').on('click', 'button', function() {
// Remove the selected card
$(this).closest('.mdl-card')
.addClass('animated zoomOut')
.one('animationend', function() {
empty_space = $('<div id="empty-space"></div>');
empty_space.css('height', ($(this).outerHeight(true)));
$(this).replaceWith(empty_space);
});
// everything is okay until now
// setTimeOut() doesn't always execute
setTimeout(function() {
console.log("test1");
// the following doesn't always happen...
$('#empty-space')
.css({
'height': '0',
'transition': 'height .3s'
// transitionend doesn't always fire either
})
.one('transitionend', function() {
$('#empty-space').remove();
console.log("test2");
});
}, 300);
// Upgrade the DOM for MDL
componentHandler.upgradeDom();
});
/* Animate.css customization */
.animated {
animation-duration: .3s
}
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet" />
<link href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css" rel="stylesheet" />
</head>
<body>
<section>
<div class="mdl-card">
<button class="mdl-button mdl-js-button">Close</button>
</div>
<p>
Content to test the height of the div above
</p>
</section>
<script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</body>
根据页面加载,什么都不会发生,有时只是第一个日志,有时它只到达 CSS 过渡,有时它是完整的。
在 Firefox 和 Chromium 上测试。
我可能误解了某些东西,因为它看起来很奇怪。
【问题讨论】:
-
带有 if 语句,其中包括其他工作操作。
-
好吧,当它不再是潜伏时,还有很多东西等着你去发现!谢谢。
-
如果您可以使用 Stack Snippets(
[<>]工具栏按钮)通过 runnable minimal reproducible example 更新问题来展示问题,那么人们会更容易帮助。 -
对不起,我误读了代码。这是
$(this).parents(".mdl-card"),而不是$(".mdl-card").parents()。对于那个很抱歉。 (如果只有一个,closest可能是更好的选择。) -
@Lucas 我已经添加了一个明确的工作解决方案。在这里给你写评论,因为它是一个未删除的答案。
标签: javascript jquery css-transitions css-animations animate.css