使用gsap实现加载进度条功能

最终动画效果

动画开始前

动画结束后

代码如下

xml 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.0/gsap.min.js"></script>
    <title>Document</title>
    <style>
        body {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .circle-progress {
            position: relative;
            width: 200px;
            height: 200px;
        }

        .circle-progress svg {
            transform: rotate(-90deg);
        }

        .circle-progress .track {
            fill: none;
            stroke: #f0f0f0;
            stroke-width: 8;
        }

        .circle-progress .progress {
            fill: none;
            stroke: #4CAF50;
            stroke-width: 8;
            stroke-linecap: round;
            stroke-dasharray: 0 314; /* 314 是周长 2 * π * 50 */
            transition: stroke-dasharray 0.5s ease;
        }

        .progress-percentage {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 24px;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div class="circle-progress">
        <svg width="100%" height="100%">
            <circle class="track" cx="50%" cy="50%" r="50"></circle>
            <circle class="progress" cx="50%" cy="50%" r="50"></circle>
        </svg>
        <!-- 添加显示百分比的元素 -->
        <div class="progress-percentage" id="progressPercentage">0%</div>
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", () => {
            const progressCircle = document.querySelector('.circle-progress .progress');
            const progressPercentage = document.getElementById('progressPercentage');
            const circleCircumference = 314; // 2 * π * 50

            gsap.to(progressCircle, {
                duration: 5,
                strokeDasharray: `${circleCircumference} ${circleCircumference}`,
                ease: "linear",
                // repeat: -1,
                yoyo: true,
                // 在动画更新时触发回调函数
                onUpdate: () => {
                    const computedDasharray = gsap.getProperty(progressCircle, "strokeDasharray");
                    const currentDash = parseFloat(computedDasharray.split(' ')[0]);
                    let progress = currentDash / circleCircumference;

                    // 处理精度误差,当进度接近 1 时,直接设置为 1
                    if (progress > 0.99) {
                        progress = 1;
                    }

                    const percentage = Math.round(progress * 100);
                    progressPercentage.textContent = `${percentage}%`;
                }
            });
        });
    </script>
</body>

</html>
相关推荐
BillKu33 分钟前
Vue3 + Element-Plus 抽屉关闭按钮居中
前端·javascript·vue.js
面向星辰1 小时前
html中css的四种定位方式
前端·css·html
Async Cipher2 小时前
CSS 权重(优先级规则)
前端·css
大怪v2 小时前
前端佬:机器学习?我也会啊!😎😎😎手“摸”手教你做个”自动驾驶“~
前端·javascript·机器学习
Liquad Li2 小时前
Angular 面试题及详细答案
前端·angular·angular.js
用户21411832636022 小时前
首发!即梦 4.0 接口开发全攻略:AI 辅助零代码实现,开源 + Docker 部署,小白也能上手
前端
gnip4 小时前
链式调用和延迟执行
前端·javascript
SoaringHeart4 小时前
Flutter组件封装:页面点击事件拦截
前端·flutter
杨天天.4 小时前
小程序原生实现音频播放器,下一首上一首切换,拖动进度条等功能
前端·javascript·小程序·音视频
Dragon Wu5 小时前
React state在setInterval里未获取最新值的问题
前端·javascript·react.js·前端框架