
在web开发中,我们经常需要动态地创建dom元素并为其添加交互行为,例如动画效果。当动画完成时,animationend事件提供了一个监听动画结束的时机,常用于触发后续操作。然而,有时即使代码看起来正确,animationend事件也可能不会如预期般触发。
考虑以下场景:通过JavaScript动态生成<img>标签并将其添加到DOM中。
displayImages() {
// 如果图片容器已存在,先删除
this.deleteExistingImageContainer();
const imageContainer = document.createElement("div");
imageContainer.id = 'imageContainer';
imageContainer.className = "mb-3";
// 载入所有图片
for (var i = 0; i < this.displayImageUrl.length; i++) {
const imageTag = document.createElement("img");
imageTag.src = this.displayImageUrl[i];
imageTag.className = 'eventImage';
imageTag.setAttribute('width', 360);
imageTag.setAttribute('height', 270);
imageContainer.appendChild(imageTag);
}
this.cateringRequestCard.appendChild(imageContainer);
// 为每个图片元素添加 animationend 事件监听器
var imageArray = imageContainer.getElementsByTagName("img");
for (var i = 0; i < imageArray.length; i++) {
console.log(imageArray[i]);
imageArray[i].addEventListener('animationend', function (e) {
console.log('animation ended');
}, false);
}
}上述JavaScript代码负责创建div#imageContainer,并在其中插入多个img.eventImage元素,随后为每个img元素绑定了animationend事件监听器。
尽管事件监听器已经绑定,但如果相应的CSS动画没有实际作用于目标元素,animationend事件将不会触发。初始的CSS代码如下:
.eventImage {
position: absolute;
}
#imageContainer:nth-of-type(1) {
border: 3px solid green;
animation-name: fader;
animation-delay: 0.5s;
animation-duration: 5s;
z-index: 20;
}
#imageContainer:nth-of-type(2) {
z-index: 10;
}
#imageContainer:nth-of-type(n+3) {
display: none;
}
@keyframes fader {
from {
opacity: 1.0;
}
to {
opacity: 0.0;
}
}在这段CSS中,定义了一个名为fader的关键帧动画,用于实现元素的淡出效果。动画被尝试应用于#imageContainer:nth-of-type(1)这个选择器所匹配的元素。
立即学习“Java免费学习笔记(深入)”;
问题症结在于CSS选择器#imageContainer:nth-of-type(1)的误用。这个选择器意图是为#imageContainer内的第一个img元素应用动画,但其实际含义是:选择文档中所有类型为div(因为#imageContainer是一个div)的元素中,ID为imageContainer且是其父元素下第一个同类型子元素的那个div。换句话说,它尝试匹配的是id="imageContainer"的div本身,并且这个div必须是其父元素的第一个div子元素。
由于动画fader旨在改变opacity,而opacity通常作用于单个图像元素,将其直接应用于整个div#imageContainer可能不是预期的行为,并且更重要的是,即使动画成功应用到容器,<img>元素上的animationend事件也无法捕获到容器的动画结束。我们真正需要动画的是imageContainer内部的第一个img元素。
要解决这个问题,我们需要修正CSS选择器,使其精确地指向#imageContainer内部的第一个<img>元素。正确的CSS选择器应该是#imageContainer img:nth-of-type(1)。
修改后的CSS代码如下:
.eventImage {
position: absolute;
}
/* 修正后的选择器:针对 #imageContainer 内的第一个 img 元素应用动画 */
#imageContainer img:nth-of-type(1) {
border: 3px solid green; /* 边框仍然可以应用于 img 元素 */
animation-name: fader;
animation-delay: 0.5s;
animation-duration: 5s;
z-index: 20;
}
#imageContainer img:nth-of-type(2) {
z-index: 10;
}
#imageContainer img:nth-of-type(n+3) {
display: none;
}
@keyframes fader {
from {
opacity: 1.0;
}
to {
opacity: 0.0;
}
}通过将选择器更改为#imageContainer img:nth-of-type(1),动画fader现在会正确地作用于id="imageContainer"的div容器内的第一个<img>子元素。一旦动画被正确应用到目标<img>元素,其上绑定的animationend事件便会按预期触发。
animationend事件不触发的问题,往往并非事件监听本身的问题,而是其前置条件——即动画是否被正确应用到目标元素——未能满足。通过本例,我们了解到精确的CSS选择器是确保动画和相关事件能够正常工作的关键。在处理动态生成元素和CSS动画时,仔细检查并理解CSS选择器的行为是避免此类问题的有效方法。
以上就是解决JavaScript动态生成元素animationend事件不触发问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号