You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
2.4 KiB
127 lines
2.4 KiB
<template>
|
|
<view :class="{ text: styleType === 'text' }"
|
|
:style="{ borderColor: styleType === 'text' ? '' : activeColor }"
|
|
class="segmented-control">
|
|
<view v-for="(item, index) in values"
|
|
:class="[{ text: styleType === 'text' }, { active: index === currentIndex }]"
|
|
:key="index" :style="{
|
|
color:
|
|
index === currentIndex
|
|
? styleType === 'text'
|
|
? activeColor
|
|
: 'rgb(255,255,255)'
|
|
: styleType === 'text'
|
|
? 'rgb(135,156,196)'
|
|
: activeColor,
|
|
backgroundColor: index === currentIndex && styleType === 'button' ? activeColor : ''
|
|
}" class="segmented-control-item" @click="_onClick(index)">
|
|
{{ item }}
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<script>
|
|
export default {
|
|
name: 'UniSegmentedControl',
|
|
props: {
|
|
current: {
|
|
type: Number,
|
|
default: 0
|
|
},
|
|
values: {
|
|
type: Array,
|
|
default () {
|
|
return []
|
|
}
|
|
},
|
|
activeColor: {
|
|
type: String,
|
|
default: 'rgb(255,255,255)'
|
|
},
|
|
styleType: {
|
|
type: String,
|
|
default: 'button'
|
|
}
|
|
},
|
|
data() {
|
|
return {
|
|
currentIndex: 0
|
|
}
|
|
},
|
|
watch: {
|
|
current(val) {
|
|
if (val !== this.currentIndex) {
|
|
this.currentIndex = val
|
|
}
|
|
}
|
|
},
|
|
created() {
|
|
this.currentIndex = this.current
|
|
},
|
|
methods: {
|
|
_onClick(index) {
|
|
if (this.currentIndex !== index) {
|
|
this.currentIndex = index
|
|
this.$emit('clickItem', index)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
@charset "UTF-8";
|
|
|
|
.segmented-control {
|
|
display: flex;
|
|
flex-direction: row;
|
|
justify-content: center;
|
|
width: 75%;
|
|
font-size: 38upx;
|
|
box-sizing: border-box;
|
|
margin: 0 auto;
|
|
overflow: hidden;
|
|
border: 1px solid;
|
|
border-radius: 10upx
|
|
}
|
|
|
|
.segmented-control.text {
|
|
border: 0;
|
|
border-radius: 0
|
|
}
|
|
|
|
.segmented-control-item {
|
|
flex: 1;
|
|
text-align: center;
|
|
line-height: 60upx;
|
|
box-sizing: border-box;
|
|
border-left: 1px solid
|
|
}
|
|
|
|
.segmented-control-item.active {
|
|
color: white;
|
|
position: relative;
|
|
}
|
|
|
|
.segmented-control-item.text {
|
|
border-left: 0;
|
|
font-size: 0.7rem;
|
|
padding-bottom:5upx;
|
|
}
|
|
|
|
.segmented-control-item.text.active:after {
|
|
content:"";
|
|
display: block;
|
|
position: absolute;
|
|
left: 36%;
|
|
bottom: -4upx;
|
|
height: 10upx;
|
|
width: 27%;
|
|
background:url(../../static/img/caitubiao.png) no-repeat;
|
|
background-size:100%;
|
|
}
|
|
|
|
.segmented-control-item:first-child {
|
|
border-left-width: 0
|
|
}
|
|
</style>
|