HTML5+CSS3+JavaScript进度条代码

目录


代码

<!--声明文档类型--> 
<!DOCTYPE html> 
<!--html标签--> 
<html> 
    <head> 
        <!--网页标题--> 
        <title>Web</title> 
        <style> 
            /*设置进度条样式*/ 
            #myProgress { 
                width: 100%; 
                background-color: #ddd; 
            }

            #myBar {
                width: 100%;
                height: 30px;
                background-color: #4CAF50;
            }
            /*设置body样式*/
            body {
                font-family: Arial, sans-serif;
                background-color: #f0f0f0;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                margin: 0;
            }
            /*设置容器样式*/
            .container {
                width: 70%;
                background-color: #fff;
                padding: 2rem;
                border-radius: 5px;
                box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
            }
            /*设置标题样式*/
            h1 {
                text-align: center;
                margin-bottom: 1rem;
            }
            /*设置表单样式*/
            form {
                display: flex;
                flex-direction: column;
            }
            /*设置标签样式*/
            label {
                margin-bottom: 0.5rem;
            }
        </style>
</head>
<body>
    <!--设置容器-->
    <div class="container">
        <!--设置标题-->
        <h1>进度条</h1>

        <!--设置进度条-->
        <div id="myProgress">
            <div id="myBar"></div>
        </div>

        <br>
        <!--设置暂停和继续按钮-->
        <button onclick="move()">&nbsp;暂停&nbsp;</button>&nbsp;
        <button onclick="begin()">&nbsp;继续&nbsp;</button><br><br>
    </div>
    <!--设置脚本-->
    <script>
        //获取进度条元素
        var elem = document.getElementById("myBar");   
        //设置进度条宽度
        var width = 1;
        //设置定时器
        var id = setInterval(frame, 20);
        //设置暂停状态
        var paused = false;
        
        //设置帧函数
        function frame() {
            //如果非暂停状态
            if (!paused) {
                //如果进度条已经达到100%
                if (width >= 100) {
                    //清除定时器
                    clearInterval(id);
                } else {
                    //增加进度条宽度
                    width++; 
                    //设置进度条元素宽度
                    elem.style.width = width + '%'; 
                }
            }
        }

        //暂停函数
        function move() {
            paused = true;
        }
        //继续函数
        function begin(){
            paused = false;
        }
    </script>
</body>
</html>

运行结果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML部分: ```html <div class="progress-ring"> <div class="progress-bar"></div> <div class="progress-value">0%</div> </div> ``` CSS部分: ```css .progress-ring { position: relative; width: 200px; height: 200px; } .progress-bar { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50%; box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1); } .progress-bar:before { position: absolute; content: ""; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50%; background-color: #1abc9c; transform: rotate(-90deg); transform-origin: center; } .progress-value { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 30px; font-weight: bold; color: #1abc9c; } ``` JavaScript部分: ```javascript const progressBar = document.querySelector(".progress-bar"); const progressValue = document.querySelector(".progress-value"); let progress = 0; const interval = setInterval(() => { progress += 1; progressBar.style.backgroundImage = "linear-gradient(" + (90 + (progress / 100) * 360) + "deg, transparent 50%, #1abc9c 50%), linear-gradient(90deg, #1abc9c 50%, transparent 50%)"; progressValue.textContent = progress + "%"; if (progress >= 100) { clearInterval(interval); } }, 50); ``` 这段 JavaScript 代码会使进度条的进度从 0% 逐渐增加到 100%。具体实现方式是通过设置 `progressBar` 元素的背景图片,利用 `linear-gradient` 渐变来实现环形进度条的效果。同时,也会动态更新进度值,将其显示在 `progressValue` 元素中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值