【发布时间】:2014-04-17 15:26:19
【问题描述】:
当我将 .shown 类添加到我的 #overlay 时,我希望不透明度淡入 2 秒,然后立即反转并淡出 2 秒,创建一种“闪烁”效果。
我尝试只删除该类,但根本没有显示任何动画。这是我的示例标记/CSS:
HTML:
<div id="outer">
This is some text
<div id="overlay"></div>
</div>
CSS:
#overlay {
...
opacity: 0;
transition: opacity 2s ease-in-out;
}
#overlay.shown {
opacity: 0.3;
}
尝试的 JS:
// Wait 2 seconds from page load...
setTimeout(function() {
// Add shown class to trigger animation
document.getElementById("overlay").classList.add("shown");
// Want to remove the class and hoped this would reverse the animation...
// it doesn't
document.getElementById("overlay").classList.remove("shown");
}, 2000);
【问题讨论】:
-
你能用 jquery 做同样的事情吗?
-
@DholakiyaAnkit 不,很遗憾没有。
-
尝试在 #overlay.shown 选择器中包含过渡属性。
标签: javascript html css