[英]Why doesn't onanimationend work in my code, but addEventListener(“animationend”) does?
I'm trying to remove an element once its fade animation has ended. 我试图在淡入淡出动画结束后删除元素。 Adding a listener to the
animationend
event works fine, but assigning the handler to .onanimationend
doesn't - the function does not fire when the animation ends. 向
animationend
事件添加一个监听器工作正常,但是将处理程序分配给.onanimationend
则不会 - 当动画结束时该函数不会触发。 Why doesn't the on-
handler work? 为什么不
on-
处理工作?
I'm running the code on FF 48, but the problem also occurs on Chrome 71. 我在FF 48上运行代码,但问题也出现在Chrome 71上。
const post = document.querySelector('.post'); const hideButton = post.children[0]; // When hide button is clicked, run fade animation. hideButton.onclick = function() { post.style.animationPlayState = "running"; }; // This function runs post.addEventListener("animationend", function() { console.log('animationend listener running'); // this.remove(); }); // This function doesn't run post.onanimationend = function() { console.log('onanimationend handler running'); // this.remove(); };
@keyframes hide { 0% { opacity: 1; height: 100%; margin: 10px 0; padding: 15px; } 75% { opacity: 0; height: 100%; margin: 10px 0; padding: 15px; } 100% { opacity: 0; height: 0px; margin: 0px; padding: 0px; } } .post { background-color: #80ff00; margin: 10px 0; padding: 15px; animation-name: hide; animation-duration: 2s; animation-fill-mode: forwards; animation-play-state: paused; }
<div class="post"> Post <button class="hide"> Hide </button> </div>
Check your browser version and compare with https://caniuse.com/#search=animationend 检查您的浏览器版本并与https://caniuse.com/#search=animationend进行比较
Firefox v48.0.0 – Java Broker 36 mins ago
Firefox v48.0.0 - Java Broker 36分钟前
Your browser isn't compatible with animationend
您的浏览器与
animationend
不兼容
This is because Chrome still doesn't and FF 45 still didn't support HTMLElement.onanimationend
handler. 这是因为Chrome仍然没有,FF 45仍然不支持
HTMLElement.onanimationend
处理程序。
I can't tell for Firefox 45 (that you should update btw), but Chrome does support the GlobalEventHandler's one ( window
), which explains why handlers attached via EventTarget.addEventListener
will catch it. 我不知道Firefox 45(您应该更新btw),但Chrome确实支持GlobalEventHandler的一个 (
window
),这解释了为什么通过EventTarget.addEventListener
附加的处理程序将捕获它。
window.onanimationend = e => { // stacksnippet's console also has CSS animations... if(e.animationName === 'roundify') console.log({ // logging the full event will kill the page target: e.target, type: e.type, animationName: e.animationName }); }
#anim { width: 100px; height: 100px; background: #333; animation: roundify 2s forwards; } @keyframes roundify { 100% { border-radius: 50%; } }
<div id="anim"></div>
Now if you really need the HTMLElement and Document handlers, you can very well implement it yourself, but note that it's generally a bad idea to change such DOM prototypes. 现在,如果你真的需要HTMLElement和Document处理程序,你可以自己很好地实现它,但请注意,改变这样的DOM原型通常是一个坏主意。
// now we only have our Element's event anim.onanimationend = e => console.log({ // logging the full event will kill the page target: e.target, type: e.type, animationName: e.animationName });
#anim{ width: 100px; height: 100px; background: #333; animation: roundify 2s forwards; } @keyframes roundify { 100% { border-radius: 50%; } }
<script> // This does modify both HTMLElement and Document protoypes, // should be placed at top most position in the doc possible (function(){ if( !("onanimationend" in HTMLElement.prototype) && window.onanimationend !== undefined ) { // will attach the handler on a the Object's `__animationendhandler` property const getsetanimationend = { get: function() { return this._animationendhandler || null }, set: function(func) { this.removeEventListener('animationend', this._animationendhandler); if(typeof func !== 'function') { this._animationendhandler = null; return; } this._animationendhandler = func; this.addEventListener('animationend', func); } }; Object.defineProperty(HTMLElement.prototype, 'onanimationend', getsetanimationend); Object.defineProperty(Document.prototype, 'onanimationend', getsetanimationend); } })(); </script> <div id="anim"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.