CSS圆圈倒计时是一种使用CSS动画和JavaScript结合实现的视觉效果,通常用于显示剩余时间。它通过创建一个圆形的进度条,并随着时间的流逝逐渐填充这个圆形,从而直观地展示倒计时。
以下是一个简单的JavaScript结合CSS实现圆圈倒计时的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Circle Countdown</title>
<style>
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
position: relative;
background-color: #eee;
}
.circle::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
background-color: #4caf50;
clip-path: polygon(50% 0%, 100% 0%, 100% 100%, 50% 100%);
transform-origin: center;
animation: rotate 10s linear infinite;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.circle::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
background-color: #eee;
clip-path: polygon(50% 0%, 100% 0%, 100% 100%, 50% 100%);
transform-origin: center;
animation: rotate 10s linear infinite;
animation-delay: 5s; /* 倒计时5秒 */
}
</style>
</head>
<body>
<div class="circle"></div>
<script>
function startCountdown(duration) {
const circle = document.querySelector('.circle');
let timeLeft = duration;
const interval = setInterval(() => {
timeLeft--;
if (timeLeft <= 0) {
clearInterval(interval);
}
}, 1000);
}
startCountdown(5); // 倒计时5秒
</script>
</body>
</html>
setInterval
或setTimeout
的时间间隔设置正确,并且考虑到浏览器的性能影响。通过以上方法,可以实现一个简单且高效的CSS圆圈倒计时效果。
领取专属 10元无门槛券
手把手带您无忧上云