【发布时间】:2021-11-21 00:01:17
【问题描述】:
我有两个盒子。当用户点击按钮时,我想fadeoutLeft我的第一个框。
但我还有一个条件。我想在 box1 淡出 75% 时显示 box2,或者换句话说,我想要一个回调函数来告诉 box1 淡出 75% 完成。
这可能吗?
目前我正在这样做
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Static Template</title>
<style>
@keyframes fadeOutLeft {
from {
opacity: 1;
}
to {
opacity: 0;
transform: translate3d(-10%, 0, 0);
}
}
.box {
width: 200px;
height: 200px;
}
.box1 {
background-color: #aff;
}
.box2 {
background-color: #eac;
opacity: 0;
}
.fadeOutLeft {
animation-name: fadeOutLeft;
animation-duration: 10000ms;
}
</style>
</head>
<body>
<div class="box box1"></div>
<div class="box box2"></div>
<button id="abc">Start Animation</button>
<script>
document.getElementById("abc").addEventListener("click", function () {
document.querySelector(".box1").classList.add("fadeOutLeft");
});
</script>
</body>
</html>
这是我的代码 https://codesandbox.io/s/cranky-resonance-5ddzt?file=/index.html:0-1077
使用 CSS 完成 75% 时我无法获得回调函数
【问题讨论】:
-
如果你不需要最高精确度,你可以设置一个7500毫秒的定时器,在定时器的回调中做你需要的。
-
@Teemu 谢谢,但我需要
first box to complete fadeout in 10s。在7500ms我想要box2 之后它开始fadeIn ..或者两个进程并行工作。 for2500` -
你不需要 JS。在第二个盒子的动画上加一个动画延迟。
标签: javascript html css css-animations requestanimationframe