[英]How to add an onClick animation?
I've been making a text adventure game with a tutorial.我一直在制作带有教程的文字冒险游戏。 The thing I want to add is to make the options I select fade out slowly.我要添加的是使选项 I select 慢慢淡出。
The thing is, if I try to change classes I break it and it is not playable anymore, because there is already a few class changes and I couldn't grasp how I could do it without breaking the game.问题是,如果我尝试更改课程,我会破坏它并且它不再可玩,因为已经有一些 class 更改,我无法理解如何在不破坏游戏的情况下做到这一点。 I can use a hover effect transition: all 1s ease-out
but what I'm trying to do is to make this happen when I click on the option, not when I hover on it.我可以使用 hover 效果transition: all 1s ease-out
但我想要做的是在我点击选项时实现这一点,而不是在我点击它时实现 hover。 I also tried to change selectOption function but it always gave an error when I add something.我还尝试更改 selectOption function 但是当我添加一些东西时它总是出错。
The html of the options: html的选项:
<div id="text">Text</div>
<div id="option-buttons" class="button-grid">
<button class="button">Option 1</button>
<button class="button">Option 2</button>
<button class="button">Option 3</button>
<button class="button">Option 4</button>
</div>
The functions and html elements of the game:游戏功能及html要素:
const textElement = document.getElementById('text')
const optionButtonsElement = document.getElementById('option-buttons')
let state = {}
function startGame() {
state = {}
showTextNode(1)
}
function showTextNode(textNodeIndex) {
const textNode = textNodes.find(textNode => textNode.id === textNodeIndex)
textElement.innerText = textNode.text
while (optionButtonsElement.firstChild) {
optionButtonsElement.removeChild(optionButtonsElement.firstChild)
}
textNode.options.forEach(option => {
if (showOption(option)) {
const button = document.createElement('button')
button.innerText = option.text
button.classList.add('button')
button.addEventListener('click', () => selectOption(option))
optionButtonsElement.appendChild(button)
}
})
}
function showOption(option) {
return option.requiredState == null || option.requiredState(state)
}
function selectOption(option) {
const nextTextNodeId = option.nextText
if (nextTextNodeId <= 0) {
return startGame()
}
state = Object.assign(state, option.setState)
showTextNode(nextTextNodeId)
}
An example of options:
const textNodes = [
{
id: 1,
text: 'You wake up in a strange place and you see a jar of blue goo near you.',
options: [
{
text: 'Take the goo',
setState: { blueGoo: true },
nextText: 2
},
{
text: 'Leave the goo',
nextText: 2
}
]
},
How can I add an onClick animation to those options?如何将 onClick animation 添加到这些选项中?
You can try to add the function/animation directly into your code.您可以尝试将函数/动画直接添加到您的代码中。 Just like this:像这样:
<div id="text">Text</div>
<div id="option-buttons" class="button-grid">
<button class="button" onclick="animation(this)">Option 1</button>
<button class="button" onclick="animation(his)">Option 2</button>
<button class="button" onclick="animation(this)">Option 3</button>
<button class="button" onclick="animation(this)">Option 4</button>
</div>
function animation (obj) {oby.class = "animation"}
You would need to specify the class animation
in CSS.您需要在 CSS 中指定 class animation
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.