javascript - How to fire "animationend" event, when I can't set a CSS animation?
问题描述
If I set the css animation to "element", it's fine. If the Css animation is not available, how does it function fire like a zero seconds animation in the ES5?
function run() { ... }
element.addEventListener('animationend', run);
Reply for
@Anurag Srivastava,
Am I wrong idea or do I have the following code wrong? Either way, the return value is "".
var el1 = document.getElementById("notAnimation");
console.log(el1.style.animation);
var el2 = document.getElementById("onAnimation");
console.log(el2.style.animation);
div {
padding: 10px;
margin: 20px;
}
#notAnimation {}
#onAnimation {
animation: scale 10s ease-in-out;
}
@keyframes scale {
0% {
transform: scale(1);
opacity: 1;
color: black;
}
50% {
transform: scale(0.95);
opacity: .4;
color: red;
}
100% {
transform: scale(1);
opacity: 1;
color: black;
}
}
<div id="notAnimation">
Not Animation
</div>
<div id="onAnimation">
Animation
</div>
解决方案
您可以检查是否包含任何值并element.style.WebkitAnimation
在值为“”时执行element.style.animation
run()
编辑结果.style
将返回 "" 任何值。你需要的是window.getComputedStyle()
连同财产animationName
。如果是none
,则没有动画,否则有。检查下面的代码:
var el1 = document.getElementById("notAnimation");
console.log(window.getComputedStyle(el1)["animationName"])
var el2 = document.getElementById("onAnimation");
console.log(window.getComputedStyle(el2)["animationName"])
div {
padding: 10px;
margin: 20px;
}
#notAnimation {}
#onAnimation {
animation: scale 10s ease-in-out;
}
@keyframes scale {
0% {
transform: scale(1);
opacity: 1;
color: black;
}
50% {
transform: scale(0.95);
opacity: .4;
color: red;
}
100% {
transform: scale(1);
opacity: 1;
color: black;
}
}
<div id="notAnimation">
Not Animation
</div>
<div id="onAnimation">
Animation
</div>
推荐阅读
- c - C中有缺陷的二分搜索
- python - 无法访问 Lambda 中 GhostScript 生成的文件
- webcam - Hololens 模拟器中的网络摄像头支持
- mysql - SpringMvc + Cloud SQL (MySQL) 集成
- rust - rustc -L 命令找不到板条箱
- vhdl - 这些代码有什么区别吗?
- c++ - 初始化多通道 OpenCV Mat
- laravel - 如何将 v-model 传递给另一个 v-model
- python-3.x - Hackerrank 列出问题 ~ 标准测试用例有效,但其他测试用例无效
- tensorflow - 如何使用 keras/tensorflow 发现方程变量的值?