前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >参考Bootstrap写的一个带百分比的进度条(附源码)

参考Bootstrap写的一个带百分比的进度条(附源码)

作者头像
德顺
发布2019-11-13 11:20:13
2.3K0
发布2019-11-13 11:20:13
举报
文章被收录于专栏:前端资源前端资源

最近需要写一个进度条的效果,网上找了一些,但都不能完美的实现需求。

于是就自己改造了一个,效果如下图:

因为动态图太大,我上传到 GitHub 了,就不在博客上再上传了。

百分比跟随进度条移动,百分比数字也随之变化。

进度条动画效果可选,颜色可根据自己的需求修改。

代码如下:

html:

代码语言:javascript
复制
<!--线性进度-->
<div class="progress">
    <div class="progress-bar progress-bar-striped active">
        <div class="progress-value">0%</div>
    </div>
</div>

JS:需要引jQuery

代码语言:javascript
复制
<script>
    var a = 0,
        timer = setInterval(function () {
            a++ //10-11秒 a+=10=>1-2秒
            $(".progress-value").html(a + "%");
            $(".progress-bar").width(a + "%");
            if (a == 80) {
                clearInterval(timer);//清除定时器
                $(".progress-bar").removeClass("active");
                location.href = "#";//跳转链接
            }
        }, 100);
</script>

CSS:

代码语言:javascript
复制
.progress {
    height: 20px;
    background-color: #f5f5f5;
    -webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1);
    overflow: visible;
    border-radius: 20px;
    margin: 80px 20px;
}

.progress .progress-bar {
    position: relative;
    float: left;
    width: 0;
    height: 100%;
    font-size: 12px;
    line-height: 20px;
    color: #fff;
    text-align: center;
    -webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .15);
    box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .15);
    -webkit-transition: width .6s ease;
    -o-transition: width .6s ease;
    transition: width .6s ease;
    border-radius: 20px;
    background-color: #001ff8;
}

.progress .progress-value {
    box-sizing: border-box;
    background-clip: content-box;
    display: block;
    height: 42px;
    line-height: 38px;
    width: 42px;
    text-align: center;
    border-radius: 50%;
    font-size: 13px;
    color: #fff;
    padding: 2px;
    background-color: #214eff;
    border: 1px solid #214eff;
    position: absolute;
    top: -60px;
    right: -15px;
}

.progress .progress-value:after {
    content: "";
    border-top: 10px solid #214eff;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    position: absolute;
    bottom: -12px;
    left: 12px;
}

/*动画效果*/
@-webkit-keyframes progress-bar-stripes {
    from {
        background-position: 35px 0;
    }
    to {
        background-position: 0 0;
    }
}

@keyframes progress-bar-stripes {
    from {
        background-position: 35px 0;
    }
    to {
        background-position: 0 0;
    }
}
/*背景效果*/
.progress-bar-striped, .progress-striped .progress-bar {
    background-image: -webkit-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
    background-image: -o-linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
    background-image: linear-gradient(45deg, rgba(255, 255, 255, .15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%, transparent 75%, transparent);
    -webkit-background-size: 35px 35px;
    background-size: 35px 35px;
}

.progress-bar.active, .progress.active .progress-bar {
    -webkit-animation: progress-bar-stripes 2s linear infinite;
    -o-animation: progress-bar-stripes 2s linear infinite;
    animation: progress-bar-stripes 2s linear infinite;
    animation-duration: 2s;
    animation-timing-function: linear;
    animation-delay: 0s;
    animation-iteration-count: infinite;
    animation-direction: normal;
    animation-fill-mode: none;
    animation-play-state: running;
    animation-name: progress-bar-stripes;
}

我将完整的页面上传到我的 GitHub 上面了,有需要的可以去下载:点击访问

声明:本文由w3h5原创,转载请注明出处:《参考Bootstrap写的一个带百分比的进度条(附源码)》 https://cloud.tencent.com/developer/article/1538024

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码如下:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档