- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
编辑:所以现在它不是随机的,看起来它总是无法从 .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 上测试。
我可能误解了一些东西,因为它看起来真的很奇怪。
最佳答案
即使您为 setTimeout
和动画提供相同的持续时间值,它们的回调的执行顺序实际上并不能保证。 简化的原因如下:
JS 本质上是单线程的,这意味着它一次可以执行一件事情。它有一个事件循环,有一堆事件队列,所有事件队列都接受网络请求、dom 事件、动画事件等事件的回调,并且在所有这些事件中,一次只运行一个并运行到结束(Run To Completion Semantic ).由于这种单线程性,进一步的复杂性是重绘和垃圾收集之类的事情也可能在此线程上运行,因此可能会发生额外的不可预测的延迟。
有用的资源:
这意味着,虽然您将空元素的高度过渡延迟到父元素缩小之后,但由于上述因素,无法始终保证此延迟的持续时间。因此,当 setTimeout
中的回调被调用时,空元素可能不存在。
如果将 setTimeout
的延迟增加到一个更大的值,空元素的高度动画实际上会更频繁地发生,因为这会增加 zoomOut< 之间的差距
动画结束和代码在 setTimeout
开始,这意味着空元素很可能在我们开始转换它的高度之前就在 DOM 中。
但是,并没有真正保证确定此延迟的最小 值的方法,因为每次都可能不同。
您必须以这样一种方式编写代码,即 animationend
和 setTimeout
回调的执行顺序无关紧要。
解决方案
首先,您不需要额外的空白空间,您可以在同一个元素上同时执行 zoomOut
动画和高度过渡。
您必须注意的一件事是,您正在使用的 css 库已经将 .mdl-card
的 min-height
设置为某个值( 200px
),所以你必须根据这个属性进行转换,因为元素的高度可能小于这个值。您还希望在 height
本身上进行转换,这样您就可以在没有任何卡顿的情况下删除元素。最后,您必须在动画和过渡完成后延迟元素的移除。
这是一个可行的解决方案:
$('section').on('click', 'button', function() {
var isAnimationDone = false,
isTransitionDone = false;
var $item = $(this).closest('.mdl-card');
$item
.addClass('animated zoomOut')
.one('animationend', function() {
isAnimationDone = true;
onAllDone();
});
$item
.css({
height: 0,
'min-height': 0
})
.one('transitionend', function() {
isTransitionDone = true;
onAllDone();
});
function onAllDone() {
if (isAnimationDone && isTransitionDone) {
$item.remove();
}
}
});
.animated {
animation-duration: 300ms
}
.mdl-card {
transition: min-height 300ms, height 300ms;
}
<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" />
<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>
<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>
有了 Promise,这会变得更容易一些:
function animate($item, animClass) {
return new Promise((resolve) => {
$item.addClass(`animated ${animClass}`).one('animationend', resolve);
});
}
function transition($item, props) {
return new Promise((resolve) => {
$item.css(props).one('transitionend', resolve);
});
}
$('section').on('click', 'button', function() {
const $item = $(this).closest('.mdl-card');
// start animation and transition simultaneously
const zoomInAnimation = animate($item, 'zoomOut');
const heightTransition = transition($item, {
height: 0,
'min-height': 0
});
// remove element once both animation and transition are finished
Promise.all([
zoomInAnimation,
heightTransition
]).then(() => $item.remove());
});
.animated {
animation-duration: 300ms
}
.mdl-card {
transition: min-height 300ms, height 300ms;
}
<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" />
<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>
<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>
关于javascript - setTimeout、jQuery 操作、transitionend 随机执行/触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41508826/
我正在尝试使用 jQuery 和 css 转换对一些动画进行排序。看起来容器宽度应该在小部件完成从“顶部”css 更改的转换之前进行动画处理,但它们是一起触发的。 为什么 widget
我想在项目上的波纹动画执行后执行一个函数。我目前正在使用以下代码: Proceed 这是一张带有图像和文本的卡片。当
我向 div 添加了一个 transitionend 事件监听器。此 div 具有在某些元素上具有过渡效果的子元素。我希望 transitionend 事件只针对我添加它的元素触发,这是一个错误吗?或
我有多个元素,每个元素都以(某种程度上)持续时间进行动画处理。我使用 CSS3 转换动画,使用 jQuery 库和来自 David Walsh 的 transitionend 辅助函数. 我的问题是
我有一些 javascript 会触发一些样式更改,从而导致 CSS 转换。 我应该如何挂接一个将在转换完成后执行的回调。显然,在旧版浏览器中,它会立即转换,但这些浏览器也无法识别 transitio
我有以下代码,我的问题是 transitionend 事件被触发了两次。我不知道是什么原因造成的。我怀疑 vendor 前缀导致了它,但事实并非如此。即使我只留下 transitionend 和 tr
我想了解为什么以下代码会以指数方式复制 transitionend 的返回值。 这对我来说没有任何意义,因为它们应该具有相同的行为并且应该只执行一次。 下面评论了预期的行为。 (function()
我需要一个特殊的类似 transitionend 的事件,该事件在所有转换完成后触发一次,或者如果 CSS 中没有定义转换则立即触发。 这是我到目前为止所想到的: (function($){ $.
在下面的示例中,我正在使用 CSS 进行 background-color 转换,并尝试处理两个 div 的 transitionend 事件。 不幸的是,transitionend 不会为 div2
我有一个 HTML 元素,我附加了一个 webkitTransitionEnd 事件。 function transEnd(event) { alert( "Finished transiti
我似乎无法让这个过渡的开始部分起作用,只有结束部分。 我之所以按照我的方式构建我的 html,是因为我需要使用 display: none 和 display:block 来实际删除元素文档流。使用
我为 CSS 关键帧动画创建了一个非常基本的测试,包括“ontransitionend”的事件监听器 - 但无论我使用什么变体,这个“过渡结束”监听器都不会触发。 (我的完整代码包含所有 vendor
我正在尝试编写一些具有 webkitTransitionEnd eventListener 回调函数的东西,但由于某种原因 eventListener 被触发了两次(doSomething 被执行了两
我有一个小应用程序,它使用 CSS3 添加和删除 .is-active 动画部分,我需要从中删除 .is-active元素 .resulting-page 然后在 transitionend 上将 .
closeContent() function 有一些问题,我不知道如何在每次单击 .show-content< 时阻止它运行. transitionend 基本上不应该踢两次。 任何帮助将不胜感激。
我想创建一个自定义事件来处理 transitionend 事件的所有不同 vendor 前缀版本(即 webkitTransitionEnd、mozTransitionEnd 、msTransitio
我有两个 div 元素,即 .parentDiv 和 .childDiv 我正在使用 css transition 来为两者设置动画。 .childDiv 应首先为其不透明度设置动画,然后在 tran
检查一下: http://jsfiddle.net/jqs4yy0p/ JS $('div').addClass('switch').on('transitionend', function(e){
我试图让 mouseover 事件监听器仅在 css 转换结束时触发。我尝试在 css 转换结束时提醒一条随机消息,它确实按预期工作(仅在结束时显示消息)。但出于某种原因,mouseover 事件甚至
我有一个切换器,我正在使用 jQuery 调用其 checked 状态。每次我单击从未选中切换到选中时,控制台都会记录 “do this!” 两次。为什么此函数会触发两次?让它触发一次的解决方法是什么
我是一名优秀的程序员,十分优秀!