日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

一起學(xué)Vue自定義組件之進(jìn)度條

 Coder編程 2021-09-24

在日常開(kāi)發(fā)中,隨著需求的個(gè)性化,邏輯的復(fù)雜化,自定義組件也變得越來(lái)越常見(jiàn),而且組件具有擴(kuò)展性強(qiáng),易復(fù)用,封裝復(fù)雜的代碼邏輯等優(yōu)點(diǎn),可以用小組件來(lái)構(gòu)建大型應(yīng)用系統(tǒng)。本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述Vue進(jìn)行組件開(kāi)發(fā)的常見(jiàn)步驟,如有不足之處,還請(qǐng)指正。

涉及知識(shí)點(diǎn)

本文案例,不僅用到了之前的基礎(chǔ)知識(shí),如:v-if, 樣式綁定,屬性傳值等,還用到了組件的特有知識(shí),如下所示:

  • 組件:可復(fù)用的Vue實(shí)例,具有封裝,復(fù)用等特點(diǎn)。
  • Vue項(xiàng)目構(gòu)建:一個(gè)vue后綴的文件,就表示一個(gè)組件。但是瀏覽器并不認(rèn)識(shí)vue文件,所有需要進(jìn)行編譯才能運(yùn)行,Vue提供了一個(gè)腳手架(CLI)來(lái)編譯運(yùn)行項(xiàng)目。
  • 組件構(gòu)成:由props(屬性),computed(計(jì)算屬性),method(方法)等構(gòu)成,來(lái)支持組件的不同需求。
  • 開(kāi)發(fā)工具:Visual Studio Code
  • 運(yùn)行工具:Vue CLI 運(yùn)行命令:vue serve

設(shè)計(jì)思路

  1. 進(jìn)度條分類:線性進(jìn)度條(采用DIV嵌套實(shí)現(xiàn)),環(huán)形進(jìn)度條(采用SVG實(shí)現(xiàn))。
  2. 提取共性特征:顏色,高度,顯示內(nèi)容,圖標(biāo)樣式等屬性值的提取,根據(jù)屬性來(lái)區(qū)分展示的,采用計(jì)算屬性實(shí)現(xiàn)。
  3. 圖標(biāo)樣式:本例中的圖標(biāo)樣式采用Wingdings字體實(shí)現(xiàn),可通過(guò)windows-->附件-->字符映射表 去查詢。

 

示例效果圖

線性進(jìn)度條:分為進(jìn)度顯示在右側(cè),還是內(nèi)側(cè)且跟隨進(jìn)度條移動(dòng)。如下所示:

 環(huán)形進(jìn)度條:大小可以設(shè)置,顯示內(nèi)容和線性進(jìn)度條一致。如下所示:

 

核心代碼

本例所有代碼進(jìn)行封裝【Progress.vue】主要包含三部分:模板(template)腳本(script)樣式(style)。

一個(gè)template下只能包含一個(gè)div,但是div下可以包含多個(gè)子節(jié)點(diǎn)。如下所示:

 1 <template>
 2     <div class="progress " :class="'progress--'+ptype">
 3         <!-- 條形進(jìn)度條 -->
 4         <div class="progress-bar" v-if="ptype==='line'">
 5             <div class="progress-bar__outer" :style="{height:strokeHeight+'px'}">
 6                 <div class="progress-bar__inner" :style="barStyle">
 7                     <!-- 進(jìn)度條內(nèi)顯示百分比 -->
 8                     <div v-if="textInside" class="progress__text" style="color:white;"> {{percentage}}% </div>
 9                 </div>
10             </div>
11            
12         </div>
13         <!-- 環(huán)形進(jìn)度條 采用SVG實(shí)現(xiàn) -->
14         <div class="progress_circle" :style="{width:cwidth+'px',height:cwidth+'px'}" v-else>
15             <svg viewBox="0 0 100 100" :style="{width:cwidth+'px',height:cwidth+'px'}">
16                 <!-- 背景圓形 -->
17                 <path :d="trackPath" fill="none" :stroke-width="relativeStrokeHeight" stroke="#e5e9f2" /> 
18                 <!-- 進(jìn)度圓形 -->
19                 <path :d="trackPath" fill="none" :stroke-width="relativeStrokeHeight" :stroke="stroke" :style="circlePathStyle" stroke-linecap="round" />
20             </svg>
21         </div>
22         <div v-if="!textInside" class="progress__text" :style="{fontSize:progressTextSize+'px'}">
23             <template v-if="!status"> {{percentage}}% </template>
24             <i v-else class="icon" :class="iconCls"></i>
25         </div>
26     </div>
27 </template>
View Code

script部分,本例主要用到props,和computed,如下所示:

 1 <script>
 2 export default {
 3     props:{
 4         strokeHeight:{
 5             // 進(jìn)度條高度
 6             // required:true,
 7             type:Number,
 8             default:10
 9         },
10         percentage:{
11             // 進(jìn)度條百分比
12             type:Number,
13             default:0,
14             required:true,
15             valiator(value){
16                 return value>=0 && value<=100
17             },
18         },
19         status:{
20             // 進(jìn)度條狀態(tài):正常狀態(tài),成功狀態(tài),異常狀態(tài)
21             type:String,
22 
23         },
24         ptype:{
25             // 進(jìn)度條樣式:條形,還是圓形
26             type:String,
27             default:'line',
28             validator:val=>['circle','line'].includes(val)
29         },
30         textInside:{
31             // 文字是否內(nèi)顯
32             type:Boolean,
33             default:false,
34         },
35         pcolor:{
36             // 進(jìn)度條顏色
37             type:String
38         },
39         cwidth:{
40             type:Number,
41             default:126,
42         }
43     },
44     computed:{
45         progressTextSize(){
46             return 9+this.strokeHeight*0.4;
47         },
48         stroke(){
49             let color;
50             if(this.pcolor){
51                 return this.pcolor;
52             }
53             switch(this.status){
54                 case 'success':
55                     color='#13ce66';
56                     break;
57                 case 'failure':
58                     color='#ff4949';
59                     break;
60                 default:
61                     color='#20a0ff';
62                     break;
63             }
64             return color;
65         },
66         barStyle(){
67             // 計(jì)算屬性調(diào)用其他計(jì)算屬性,必須加this關(guān)鍵字,否則找不到
68             return {width:this.percentage+'%',backgroundColor:this.stroke}
69         },
70         iconCls(){
71             if( this.ptype ==='line'){
72                 // 如果是線性進(jìn)度條
73                 return this.status ==='success'?'icon-circle-check':'icon-circle-close';
74             }else{
75                 return this.status ==='success'?'icon-check':'icon-close';
76             }
77         },
78         trackPath(){
79             const radius = 50-this.relativeStrokeHeight/2;
80             return 'M 50 50 m 0 -'+radius+' a '+radius+' '+radius+' 0 1 1 0 '+radius*2+' a '+radius+' '+radius+' 0 1 1 0 -'+radius*2+' ' ; 
81         },
82         relativeStrokeHeight(){
83             return this.strokeHeight*100 / this.cwidth;
84         },
85         perimeter(){
86             const radius = 50-this.relativeStrokeHeight/2;
87             return 2*Math.PI*radius;
88         },
89         circlePathStyle(){
90             const perimeter = this.perimeter;
91             return{
92                 strokeDasharray:''+perimeter+'px,'+perimeter+'px',
93                 strokeDashoffset:(1-this.percentage/100)*perimeter+'px',
94 
95             }
96         }
97     }
98 }
99 </script>
View Code

style部分,本例使用了偽元素(::before)顯示圖標(biāo),如下所示:

 1 <style>
 2     .progress{
 3         margin: 10px;
 4         /* border: 1px solid #ffbbff; */
 5     }
 6     .progress-bar{
 7         display:inline-block;
 8         width: 98%;
 9         box-sizing: border-box; /* 盒模型的方式 */
10         margin-right: -50px;
11         padding-right: 50px;
12     }
13     .progress-bar__outer{
14         width: 100%;
15         border-radius: 10px;
16         background-color: #ebeef5;
17     }
18     .progress-bar__inner{
19         /* width: 60%; */
20         background-color: rebeccapurple;
21         border-radius: 10px;
22         height: 100%;
23         transition: width 0.6s ease;
24         text-align: right;
25         line-height: 80%;
26     }
27     .progress__text{
28         font-size: 12px;
29         margin-left: 6px;
30         display: inline-block;
31         vertical-align: middle;
32         margin-right: 5px;
33     }
34     .icon-circle-close,.icon-close{
35         font-family: 'Wingdings' !important;
36         color:red;
37     }
38     .icon-circle-check,.icon-check{
39         font-family: 'Wingdings' !important;
40         color:seagreen;
41     }
42     
43     .icon-circle-close::before{
44         content: '\FD';
45     }
46     .icon-close::before{
47         content: '\FB';
48     }
49     .icon-circle-check::before{
50         content: '\FE';
51     }
52     .icon-check::before{
53         content: '\FC';
54     }
55 
56     .progress_circle{
57         /* 環(huán)形進(jìn)度條 */
58     }
59     .progress--circle{
60         display: inline-block;
61         position: relative;
62     }
63 
64     .progress--circle .progress__text{
65         position:absolute;
66         top:50%;
67         transform: translateY(-50%);
68         margin-left: 0px;
69         text-align: center;
70         width: 100%;
71     }
72 
73 </style>
View Code

組件調(diào)用源碼

首先引入組件,并注冊(cè)組件,如下所示:

 1 <script>
 2 // 組件名稱大小寫(xiě)敏感,不能和已經(jīng)存在的HTML里面的控件同名,如果是駝峰形式,則可以用短橫線的方式(如:d=progress)或者和名稱保持一致
 3 import dProgress from './Progress';
 4 export default {
 5   components:{
 6     dProgress
 7   },
 8 
 9 }
10 </script>
View Code

然后進(jìn)行調(diào)用即可,如下所示:

 1 <template>
 2   
 3   <div>
 4     <H2>線性進(jìn)度條--百分比外顯</H2>
 5     <dProgress :percentage="0" />
 6     <dProgress :percentage="40" pcolor="orange" />
 7     <dProgress :percentage="60" status="success" />
 8     <dProgress :percentage="80" status="failure" />
 9     <H2>線性進(jìn)度條--百分比內(nèi)顯</H2>
10     <dProgress :percentage="0" :text-inside="true" :stroke-height="16"/>
11     <dProgress :percentage="40" :text-inside="true"  :stroke-height="16"/>
12     <dProgress :percentage="60" status="success" :text-inside="true"  :stroke-height="16"/>
13     <dProgress :percentage="80" status="failure" :text-inside="true"  :stroke-height="16"/>
14     <h2>環(huán)形進(jìn)度條</h2>
15     <dProgress :percentage="0" ptype="circle" />
16     <dProgress :percentage="40" ptype="circle" />
17     <dProgress :percentage="60" ptype="circle" status="success" />
18     <dProgress :percentage="80" ptype="circle" status="failure" />
19   </div>
20 </template>
View Code

如需本例完整代碼,可以點(diǎn)擊代碼鏈接進(jìn)行下載

代碼鏈接

備注

 

【曲玉管】

作者:柳永[宋]

隴首云飛,江邊日晚,煙波滿目憑闌久。

一望關(guān)河蕭索,千里清秋,忍凝眸?

杳杳神京,盈盈仙子,別來(lái)錦字終難偶。

斷雁無(wú)憑,冉冉飛下汀洲,思悠悠。

 

暗想當(dāng)初,有多少、幽歡佳會(huì),

豈知聚散難期,翻成雨恨云愁?

阻追游。每登山臨水,惹起平生心事,

一場(chǎng)消黯,永日無(wú)言,卻下層樓。

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多