前言
uview是我目前遇到的,注释写的比较好的组件库。没有之一。平时在工作中,就是凭借着业务组件,然后自己开始完成业务。最近遇到了vue的项目,业务上也要用到这个u-line-progress组件。于是,我把这个组件改了一个,也能支持在vue项目中能用。如果说这个组件有什么缺陷,那么我目前看出来的就是block没有改为tempalte。具体原因
<template>
<view class="u-progress" :style="{
borderRadius: round ? '100rpx' : 0,
height: height + 'rpx',
backgroundColor: inactiveColor
}">
<view :class="[
type ? `u-type-${type}-bg` : '',
striped ? 'u-striped' : '',
]" class="u-active" :style="[progressStyle]">
<slot v-if="$slots.default || $slots.$default" />
<template v-else-if="showPercent">
{{percent + '%'}}
</template>
</view>
</view>
</template>
我们可以分为三部分来读,第一部分,我们可以来看看template里面的内容。可以看到用了数据绑定来操作CSS class和内联样式。CSS class和内联样式都支持对象和数组的形式。只要我们合理利用好vue的特性。里面会看到有一个slot这样的元素,这里的slot是没有带有name不是具名插槽。获取他的就是default.$slots.$default这个其实是可以去掉的。没有在官网上面找到这个API出自哪里。如果有知道的小伙伴请联系一下我。
<script>
/**
* lineProgress 线型进度条
* @description 展示操作或任务的当前进度,比如上传文件,是一个线形的进度条。
* @tutorial https://www.uviewui.com/components/lineProgress.html
* @property {String Number} percent 进度条百分比值,为数值类型,0-100
* @property {Boolean} round 进度条两端是否为半圆(默认true)
* @property {String} type 如设置,active-color值将会失效
* @property {String} active-color 进度条激活部分的颜色(默认#19be6b)
* @property {String} inactive-color 进度条的底色(默认#ececec)
* @property {Boolean} show-percent 是否在进度条内部显示当前的百分比值数值(默认true)
* @property {String Number} height 进度条的高度,单位rpx(默认28)
* @property {Boolean} striped 是否显示进度条激活部分的条纹(默认false)
* @property {Boolean} striped-active 条纹是否具有动态效果(默认false)
* @example <u-line-progress :percent="70" :show-percent="true"></u-line-progress>
*/
export default {
name: "u-line-progress",
props: {
// 两端是否显示半圆形
round: {
type: Boolean,
default: true
},
// 主题颜色
type: {
type: String,
default: ''
},
// 激活部分的颜色
activeColor: {
type: String,
default: '#19be6b'
},
inactiveColor: {
type: String,
default: '#ececec'
},
// 进度百分比,数值
percent: {
type: Number,
default: 0
},
// 是否在进度条内部显示百分比的值
showPercent: {
type: Boolean,
default: true
},
// 进度条的高度,单位rpx
height: {
type: [Number, String],
default: 28
},
// 是否显示条纹
striped: {
type: Boolean,
default: false
},
// 条纹是否显示活动状态
stripedActive: {
type: Boolean,
default: false
}
},
computed: {
progressStyle() {
let style = {};
style.width = this.percent + '%';
if(this.activeColor) style.backgroundColor = this.activeColor;
return style;
}
}
}
</script>
<style lang="scss" scoped>
@import "../../libs/css/style.components.scss";
.u-progress {
overflow: hidden;
height: 15px;
/* #ifndef APP-NVUE */
display: inline-flex;
/* #endif */
align-items: center;
width: 100%;
border-radius: 100rpx;
}
.u-active {
width: 0;
height: 100%;
align-items: center;
@include vue-flex;
justify-items: flex-end;
justify-content: space-around;
font-size: 20rpx;
color: #ffffff;
transition: all 0.4s ease;
}
.u-striped {
background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
background-size: 39px 39px;
}
.u-striped-active {
animation: progress-stripes 2s linear infinite;
}
@keyframes progress-stripes {
0% {
background-position: 0 0;
}
100% {
background-position: 39px 0;
}
}
</style>
第二部分,看script里面的内容,其实已经作者已经描述的很详细了,我不再赘述。第三部分的内容时css部分,uniapp本身带有条件编译功能。还用了一些scss的特性。比如@include关键字。渐变还是比较难的,我到目前还是没有搞太熟练。还用了一个css的动画。看完还是很有收获的。
最后
我们已经读完了一个组件库的一小部分代码了,已经很厉害了。如果有疑问,请留言。