我想使用带有“transitionend”的 EventListenner 链接多个图像,例如我有一个石头剪刀布游戏,每次单击其中一个按钮播放时,我希望图像从石头旋转,到纸,最后到剪刀,每次都等待过渡结束,然后再次交换图像,模拟现实生活中的游戏交互。我已经设法链接其中一个转换,但我坚持如何为多个连续"transitionend"事件执行此操作的逻辑
let images = ['https://adiihd.github.io/rock-paper-scissors-game/img/rock.png', 'https://adiihd.github.io/rock-paper-scissors-game/img/paper.png', 'https://adiihd.github.io/rock-paper-scissors-game/img/scissors.png'];
const buttons = document.querySelectorAll('button');
// we use the .forEach method to iterate through each button
buttons.forEach((button) => {
// and for each one we add a 'click' listener
button.addEventListener('click', (e) => {
let userImg = document.querySelector("#userimg");
userImg.classList.add("playing");
userImg.addEventListener('transitionend', () => {
userImg.classList.remove("playing");
userImg.setAttribute('src', images[1]);
});
userImg.addEventListener('transitionend', () => {
userImg.classList.remove("playing");
userImg.setAttribute('src', images[2]);
});
});
});
#userimg {
transition: all 0.4s ease;
width: 200px;
}
.playing {
transform: rotate(30deg);
}
<img id="userimg" src="https://adiihd.github.io/rock-paper-scissors-game/img/rock.png">
<br>
<button id="Rock">Rock</button>
<button id="Paper">Paper</button>
<button id="Scissors">Scissors</button>
qq_笑_17
相关分类