要使用JavaScript设置动画的持续时间,可以通过以下步骤实现:
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
var element = document.getElementById("myElement");
element.style.transitionDuration = "2s";
element.classList.add("fade-in");
完整的示例代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fade-in {
animation: fadeIn 2s;
}
</style>
</head>
<body>
<div id="myElement">Hello, world!</div>
<script>
var element = document.getElementById("myElement");
element.style.transitionDuration = "2s";
element.classList.add("fade-in");
</script>
</body>
</html>
这样,当页面加载时,元素"myElement"将以2秒的持续时间淡入显示。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云