123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <template>
- <view class="s-switch-container">
- <view class="s-switch-item" :class="{
- 's-switch-selected': item.selected
- }"
- @click="selectItem(item)"
- v-for="item in switchArr" :key="item.key">
- {{item.value}}
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'Switch',
- props: {
- defaultValue: {
- type: Number,
- default: 0
- },
- switchList: {
- type: Array,
- default: [
- {
- key: 0,
- value: '是'
- },
- {
- key: 1,
- value: '否'
- }
- ]
- }
- },
- data () {
- return {
- switchArr: []
- }
- },
- methods: {
- selectItem (info) {
- if (info.selected === false) {
- for (const item of this.switchArr) {
- if (info.key === item.key) {
- item.selected = true
- this.$emit('switchValue', item)
- } else {
- item.selected = false
- }
- }
- }
- }
- },
- created () {
- for (let i = 0; i < 2; i++) {
- const arrItem = {
- key: this.switchList[i].key,
- value: this.switchList[i].value,
- selected: this.defaultValue === this.switchList[i].key ? true : false
- }
- if (this.switchList[i].key === this.defaultValue) {
- this.$emit('switchValue', arrItem)
- }
- this.switchArr.push(arrItem)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .s-switch-container {
- padding: 2upx;
- display: flex;
- flex-direction: row;
- width: 160upx;
- height: 60upx;
- border-radius: 100upx;
- border: 4upx solid #f37b1d;
- }
- .s-switch-item {
- color: #f37b1d;
- font-size: 30upx;
- width: 80upx;
- display: flex;
- justify-content: center;
- align-items: center;
- }
- .s-switch-selected {
- color: #ffffff;
- border-radius: 40upx;
- background: #f37b1d;
- }
- </style>
|