每次用户单击然后重新开始时,如何使用按钮启用 CSS 动画
How to enable CSS animation with a button everytime when the user clicks on then it starts over
我只知道怎么做一次。但我希望当用户再次点击它时动画重新开始或重新开始。
我有 thiese:
function animation() {
document.getElementById('ExampleButton').className = 'Animation';
}
.ExampleButtonCSS {
position: fixed;
bottom: 113px;
background: rgba(0, 0, 0, 0);
color: #cfcfcf;
width: 100%;
padding-left: 20px;
}
#ExampleButton {
cursor: pointer;
font-size: 15px;
outline-color: #000;
outline-style: auto;
outline-width: 2px;
padding: 5px;
border: none;
background: rgba(0, 0, 0, 0.25);
color: #cfcfcf;
opacity: 0;
}
.Animation {
opacity: 0;
animation: inactivity 5s ease normal;
}
@keyframes inactivity {
from {
opacity: 0;
}
10% {
opacity: 1;
}
90% {
opacity: 1;
}
to {
opacity: 0;
}
}
<div class="ExampleButtonCSS">
<button id="ExampleButton" onclick="animation()">Fade in and Out</button>
</div>
所以,如果我点击它,我该怎么做,如果它已经在动画中,它会重新开始,或者如果它已经在任何时候结束,它会重新开始
我不太明白你想要什么。
但是您可以在动画完成后重置元素的 class。
function animation() {
document.getElementById('ExampleButton').className = '';
document.getElementById('ExampleButton').className = 'Animation';
setTimeout(() => {
document.getElementById('ExampleButton').className = '';
}, 5000)
}
.ExampleButtonCSS {
position: fixed;
bottom: 113px;
background: rgba(0, 0, 0, 0);
color: #cfcfcf;
width: 100%;
padding-left: 20px;
}
#ExampleButton {
cursor: pointer;
font-size: 15px;
outline-color: #000;
outline-style: auto;
outline-width: 2px;
padding: 5px;
border: none;
background: rgba(0, 0, 0, 0.25);
color: #cfcfcf;
}
.Animation {
opacity: 0;
animation: inactivity 5s ease normal;
}
@keyframes inactivity {
from {
opacity: 0;
}
10% {
opacity: 1;
}
90% {
opacity: 1;
}
to {
opacity: 0;
}
}
<div class="ExampleButtonCSS">
<button id="ExampleButton" onclick="animation()">Fade in and Out</button>
</div>
你需要使用animationend event
<div class="ExampleButtonCSS">
<button id="ExampleButton"> Fade in and Out </button>
</div>
const explmButton = document.getElementById('ExampleButton')
explmButton.onclick = () =>
{
explmButton.classList.add('Animation')
}
explmButton.onanimationend = () =>
{
explmButton.classList.remove('Animation')
}
我只知道怎么做一次。但我希望当用户再次点击它时动画重新开始或重新开始。
我有 thiese:
function animation() {
document.getElementById('ExampleButton').className = 'Animation';
}
.ExampleButtonCSS {
position: fixed;
bottom: 113px;
background: rgba(0, 0, 0, 0);
color: #cfcfcf;
width: 100%;
padding-left: 20px;
}
#ExampleButton {
cursor: pointer;
font-size: 15px;
outline-color: #000;
outline-style: auto;
outline-width: 2px;
padding: 5px;
border: none;
background: rgba(0, 0, 0, 0.25);
color: #cfcfcf;
opacity: 0;
}
.Animation {
opacity: 0;
animation: inactivity 5s ease normal;
}
@keyframes inactivity {
from {
opacity: 0;
}
10% {
opacity: 1;
}
90% {
opacity: 1;
}
to {
opacity: 0;
}
}
<div class="ExampleButtonCSS">
<button id="ExampleButton" onclick="animation()">Fade in and Out</button>
</div>
所以,如果我点击它,我该怎么做,如果它已经在动画中,它会重新开始,或者如果它已经在任何时候结束,它会重新开始
我不太明白你想要什么。
但是您可以在动画完成后重置元素的 class。
function animation() {
document.getElementById('ExampleButton').className = '';
document.getElementById('ExampleButton').className = 'Animation';
setTimeout(() => {
document.getElementById('ExampleButton').className = '';
}, 5000)
}
.ExampleButtonCSS {
position: fixed;
bottom: 113px;
background: rgba(0, 0, 0, 0);
color: #cfcfcf;
width: 100%;
padding-left: 20px;
}
#ExampleButton {
cursor: pointer;
font-size: 15px;
outline-color: #000;
outline-style: auto;
outline-width: 2px;
padding: 5px;
border: none;
background: rgba(0, 0, 0, 0.25);
color: #cfcfcf;
}
.Animation {
opacity: 0;
animation: inactivity 5s ease normal;
}
@keyframes inactivity {
from {
opacity: 0;
}
10% {
opacity: 1;
}
90% {
opacity: 1;
}
to {
opacity: 0;
}
}
<div class="ExampleButtonCSS">
<button id="ExampleButton" onclick="animation()">Fade in and Out</button>
</div>
你需要使用animationend event
<div class="ExampleButtonCSS">
<button id="ExampleButton"> Fade in and Out </button>
</div>
const explmButton = document.getElementById('ExampleButton')
explmButton.onclick = () =>
{
explmButton.classList.add('Animation')
}
explmButton.onanimationend = () =>
{
explmButton.classList.remove('Animation')
}