链接多个 "transitionend" 个 EventListener
Chaining multiple "transitionend" EventListener's
我想使用带有“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>
等待 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', async (e) => {
let userImg = document.querySelector(".userimg");
userImg.classList.remove("userimg-animation");
// Force a reflow, see https://css-tricks.com/restart-css-animation/
userImg.offsetWidth;
userImg.classList.add("userimg-animation");
userImg.setAttribute("choice", button.id.toLowerCase());
});
});
.imageHolder {
position: relative;
display: inline-block;
}
.spacerimg {
opacity: 0;
}
.userimg-animation {
width: 100%;
height: 100%;
position: absolute;
animation-name: rockpaperscissors;
animation-duration: 1.2s;
}
.userimg {
background-repeat: no-repeat!important;
}
.userimg[choice="rock"] {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/rock.png);
}
.userimg[choice="paper"] {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/paper.png);
}
.userimg[choice="scissors"] {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/scissors.png);
}
@keyframes rockpaperscissors {
0% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/rock.png);
}
33.2% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/rock.png);
}
33.3% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/paper.png);
}
66.5% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/paper.png);
}
66.6% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/scissors.png);
}
99.9% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/scissors.png);
}
100% {
background: none;
}
}
<div class="imageHolder">
<div class="userimg"></div>
<img class="spacerimg" src="https://adiihd.github.io/rock-paper-scissors-game/img/rock.png">
</div>
<br>
<button id="Rock">Rock</button>
<button id="Paper">Paper</button>
<button id="Scissors">Scissors</button>
注意:我不得不编写额外的关键帧以防止背景之间的动画(它们在彼此之间淡入淡出)。然而,您可能需要动画,所以如果您删除每个奇数关键帧,您将拥有更少的 CSS 并且它们将在彼此之间进行动画处理。
我想使用带有“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>
等待 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', async (e) => {
let userImg = document.querySelector(".userimg");
userImg.classList.remove("userimg-animation");
// Force a reflow, see https://css-tricks.com/restart-css-animation/
userImg.offsetWidth;
userImg.classList.add("userimg-animation");
userImg.setAttribute("choice", button.id.toLowerCase());
});
});
.imageHolder {
position: relative;
display: inline-block;
}
.spacerimg {
opacity: 0;
}
.userimg-animation {
width: 100%;
height: 100%;
position: absolute;
animation-name: rockpaperscissors;
animation-duration: 1.2s;
}
.userimg {
background-repeat: no-repeat!important;
}
.userimg[choice="rock"] {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/rock.png);
}
.userimg[choice="paper"] {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/paper.png);
}
.userimg[choice="scissors"] {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/scissors.png);
}
@keyframes rockpaperscissors {
0% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/rock.png);
}
33.2% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/rock.png);
}
33.3% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/paper.png);
}
66.5% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/paper.png);
}
66.6% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/scissors.png);
}
99.9% {
background: url(https://adiihd.github.io/rock-paper-scissors-game/img/scissors.png);
}
100% {
background: none;
}
}
<div class="imageHolder">
<div class="userimg"></div>
<img class="spacerimg" src="https://adiihd.github.io/rock-paper-scissors-game/img/rock.png">
</div>
<br>
<button id="Rock">Rock</button>
<button id="Paper">Paper</button>
<button id="Scissors">Scissors</button>
注意:我不得不编写额外的关键帧以防止背景之间的动画(它们在彼此之间淡入淡出)。然而,您可能需要动画,所以如果您删除每个奇数关键帧,您将拥有更少的 CSS 并且它们将在彼此之间进行动画处理。