void element.offsetWidth;
是一行奇怪的代码,它似乎什么也不做,但却是 CSS 动画工作所必需的。这条线有什么作用以及为什么需要它?
如果整行被注释掉,动画会发生一次但不会重复。void
(如果删除它仍然有效。)
完整的代码/演示在这里;我尝试复制以下相关摘录:
脚本.js:
beatIndicator = document.getElementById("beatIndicator"),
...
// Happens every time a beat starts:
beatIndicator.classList.remove("anim");
void beatIndicator.offsetWidth; // Why is this line needed?
beatIndicator.classList.add("anim");
示例-高级.html
<span style="display: none;" id="beatIndicator" class="pulse"></span>
<style>
.pulse {
width: 40px;
height: 40px;
border-radius: 50%;
background: #f44336;
z-index: 3;
left: -18px;
display: inline-block;
vertical-align: middle;
margin-left: 10px;
}
@keyframes pulse-anim {
from {
box-shadow: rgba(244, 67, 54, 1) 0px 0px 0px 0px;
}
to {
box-shadow: rgba(244, 67, 54, 0) 0px 0px 0px 14px;
}
}
.pulse.anim {
animation: pulse-anim;
}
</style>
泛舟湖上清波郎朗
相关分类