今天給大家?guī)硪粋€(gè)比較炫的進(jìn)度條,進(jìn)度條在一耗時(shí)操作上給用戶一個(gè)比較好的體驗(yàn),不會(huì)讓用戶覺得在盲目等待,對(duì)于沒有進(jìn)度條的長時(shí)間等待,用戶會(huì)任務(wù)死機(jī)了,毫不猶豫的關(guān)掉應(yīng)用;一般用于下載任務(wù),刪除大量任務(wù),網(wǎng)頁加載等;如果有使用HTML5為手機(jī)布局的,也可以用于手機(jī)中~
效果圖:
1、html結(jié)構(gòu):
<div id="loadBar01" class="loadBar"> <div> <span class="percent"> <i></i> </span> </div> <span class="percentNum">0%</span> </div>
簡單分析下:
div.loadBar代表整個(gè)進(jìn)度條
div.loadBar div 設(shè)置了圓角表框 ,div.loadBar div span 為進(jìn)度 (動(dòng)態(tài)改變寬度), div.loadBar div span i 為進(jìn)度填充背景色(即width=100%)
HTML的結(jié)構(gòu),大家可以自己設(shè)計(jì),只要合理,都沒有問題~
2、CSS:
body { font-family: Thoma, Microsoft YaHei, 'Lato', Calibri, Arial, sans-serif; } #content { margin: 120px auto; width: 80%; } .loadBar { width: 600px; height: 30px; border: 3px solid #212121; border-radius: 20px; position: relative; } .loadBar div { width: 100%; height: 100%; position: absolute; top: 0; left: 0; } .loadBar div span, .loadBar div i { box-shadow: inset 0 -2px 6px rgba(0, 0, 0, .4); width: 0%; display: block; height: 100%; position: absolute; top: 0; left: 0; border-radius: 20px; } .loadBar div i { width: 100%; -webkit-animation: move .8s linear infinite; background: -webkit-linear-gradient(left top, #7ed047 0%, #7ed047 25%, #4ea018 25%, #4ea018 50%, #7ed047 50%, #7ed047 75%, #4ea018 75%, #4ea018 100%); background-size: 40px 40px; } .loadBar .percentNum { position: absolute; top: 100%; right: 10%; padding: 1px 15px; border-bottom-left-radius: 16px; border-bottom-right-radius: 16px; border: 1px solid #222; background-color: #222; color: #fff; } @-webkit-keyframes move { 0% { background-position: 0 0; } 100% { background-position: 40px 0; } }
此時(shí)效果為:
整體布局就是利用position relative和absolute~
比較難的地方就是,漸變條的實(shí)現(xiàn):
我們采用
a、從左上到右下的漸變
b、顏色分別為:0-25% 為#7ed047 , 25%-50% 為#4ea018 , 50%-75%為#7ed047 , 75%-100%為#4ea018
c、背景的大小為40px 40px 這個(gè)設(shè)置超過高度就行, 越大,條文寬度越寬
分析圖:
設(shè)置的原理就是上圖了,同時(shí)可以背景寬度設(shè)置越大,條文寬度越大;
3、設(shè)置Js,創(chuàng)建LoadBar對(duì)象
function LoadingBar(id) { this.loadbar = $("#" + id); this.percentEle = $(".percent", this.loadbar); this.percentNumEle = $(".percentNum", this.loadbar); this.max = 100; this.currentProgress = 0; } LoadingBar.prototype = { constructor: LoadingBar, setMax: function (maxVal) { this.max = maxVal; }, setProgress: function (val) { if (val >= this.max) { val = this.max; } this.currentProgress = parseInt((val / this.max) * 100) + "%"; this.percentEle.width(this.currentProgress); this.percentNumEle.text(this.currentProgress); } };
我們創(chuàng)建了一個(gè)LoadBar對(duì)象,同時(shí)公開了兩個(gè)方法,一個(gè)設(shè)置最大進(jìn)度,一個(gè)設(shè)置當(dāng)前進(jìn)度;比如下載文件最大進(jìn)度為文件大小,當(dāng)前進(jìn)度為已下載文件大小。
4、測試