使用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>
相关推荐
澄江静如练_11 小时前
优惠券提示文案表单项(原生div写的)
前端·javascript·vue.js
C_心欲无痕11 小时前
ts - 关于Object、object 和 {} 的解析与区别
开发语言·前端·javascript·typescript
L Jiawen11 小时前
【Windows 系统】Chrome浏览器退出登录状态失效
前端·chrome·windows
IT_陈寒11 小时前
Java并发编程实战:从入门到精通的5个关键技巧,让我薪资涨了40%
前端·人工智能·后端
全栈前端老曹11 小时前
【包管理】read-pkg-up 快速上手教程 - 读取最近的 package.json 文件
前端·javascript·npm·node.js·json·nrm·package.json
程序员爱钓鱼12 小时前
Node.js 编程实战:测试与调试 —— 调试技巧与性能分析
前端·后端·node.js
JQLvopkk12 小时前
Vue框架技术详细介绍及阐述
前端·javascript·vue.js
vyuvyucd12 小时前
插件式开发:C++与C#实战指南
java·前端·数据库
C_心欲无痕12 小时前
ts - 类型收窄
前端·typescript
笔COOL创始人12 小时前
requestAnimationFrame 动画优化实践指南
前端·javascript·面试