我有像这样的div。H1标记根据按钮点击动态显示数据。
<div id="quoteId" >
<h1>{{ data }}</h1>
</div>
<button>Show Next</button>使用css3动画效果作为
#quoteId {
opacity: 1;
animation-name: example;
animation-duration: 1s;
}
@keyframes example {
0% {opacity: 0;}
50% {opacity: 1;}
100% {opacity: 1;}
}它是在第一时间工作的。但问题是,当我们每次点击按钮时,如何实现同样的css效果。如果有什么需要澄清的,请告诉我。提前谢谢。
发布于 2018-05-23 11:11:10
您可以像这样重复使用动画:
let but = document.getElementById("button");
let div = document.getElementById("quoteId");
but.addEventListener('click', function(){
div.classList.add("quote");
setTimeout(() => div.classList.remove("quote"), 1000);
});.quote {
opacity: 1;
animation-name: example;
animation-duration: 1s;
}
@keyframes example {
0% {opacity: 0;}
50% {opacity: 1;}
100% {opacity: 1;}
}<div id="quoteId">
<h1>{{ data }}</h1>
</div>
<button id="button">Show Next</button>
发布于 2018-05-23 11:18:37
带有jQuery的A) :
$("#quoteId").toggleClass('anime reve');
$(document).ready(function(){
$('button').click(function(){
$("#quoteId").toggleClass('anime reve');
})
}).anime {
animation-name: example;
animation-duration: 5s;
}
.reve {
animation-name: reve;
animation-duration: 5s;
}
@keyframes example {
0% {opacity: 0;}
50% {opacity: 1;}
100% {opacity: 1;}
}
@keyframes reve {
0% {opacity: 0;}
50% {opacity: 1;}
100% {opacity: 1;}
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="quoteId" class="reve" >
<h1>{{ data }}</h1>
</div>
<button>Show Next</button>
带有纯Css的B) :
注意::active选择器仅在按下按钮时应用。
你必须在使用active选择器修改html代码后再做一点修改。
button:active + #quoteId {
animation-name: example;
animation-duration: 20s;
}
@keyframes example {
0% {opacity: 0;}
50% {opacity: 1;}
100% {opacity: 1;}
}
button:active + #quoteId {
animation-name: example;
animation-duration: 1s;
}
.container {
position: relative;
padding-bottom: 20px;
}
button {
position: absolute;
bottom: 0;
}<div class="container">
<button>Show Next</button>
<div id="quoteId">
<h1>{{ data }}</h1>
</div>
</div>
https://stackoverflow.com/questions/50478972
复制相似问题