subsection 分段器组件

属性

  • list: @Array 内容数组
  • width: @Number 滑块宽度
  • height: @Number 滑块高度
  • topBottom: @Number 上下间距
  • leftRight: @Number 左右间距
  • color: @String文字颜色
  • activeColor: @String选中文字颜色
  • bgColor: @String背景颜色
  • activeBgColor: @String滑块背景颜色
  • transitionTime: @Number 动画时间

展示

在这里插入图片描述

完整代码

<template>
    <div class="switch" :style="switchStyles">
        <ul class="switch__item" :style="switchItemStyles">
            <li class="switch__item__text"
                v-for="(item, index) in list" :key="index"
                :style="{
                    ...switchItemTextStyles,
                    ...{color: current == index ? activeColor : color}
                }"
                @click="change(index)"
            >{{ item.name }}</li>
        </ul>
        <div class="switch__action" :style="switchActionStyles"></div>
    </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
    name: 'slider-button',
    props: {
        // 内容数组
        list: {
            type: Array,
            default() {
                return [
                    {
                        name: '固定模式',
                        value: 0
                    },
                    {
                        name: '缩放模式',
                        value: 1
                    }
                ]
            }
        },
        // 滑块宽度
        width: {
            type: Number,
            default: 114
        },
        // 滑块高度
        height: {
            type: Number,
            default: 34
        },
        // 上下间距
        topBottom: {
            type: Number,
            default: 3
        },
        // 左右间距
        leftRight: {
            type: Number,
            default: 3
        },
        // 文字颜色
        color: {
            type: String,
            default: '#858585'
        },
        // 选中文字颜色
        activeColor: {
            type: String,
            default: '#FFFFFF'
        },
        // 背景颜色
        bgColor: {
            type: String,
            default: '#EDEDED'
        },
        // 滑块背景颜色
        activeBgColor: {
            type: String,
            default: '#F4A221'
        },
        // 动画时间
        transitionTime: {
            type: [Number, String],
            default: .5
        }
    },
    data() {
        return {
            current: 0
        }
    },
    computed: {
        ...mapState('map', ['mapType']),
        switchStyles() {
            return {
                width: `${this.width * this.list.length + this.leftRight * 2}px`,
                height: `${this.height + this.topBottom * 2}px`,
                borderRadius: `${this.height + this.topBottom * 2 / 2}px`,
                background: this.bgColor,
                padding: `${this.topBottom} ${this.leftRight}px`
            }
        },
        switchItemStyles() {
            return {
                width: `${this.width * this.list.length}px`,
                top: `${this.topBottom}px`,
                bottom: `${this.topBottom}px`,
                left: `${this.leftRight}px`,
                right: `${this.leftRight}px`
            }
        },
        switchItemTextStyles() {
            return {
                width: `${this.width}px`,
                height: `${this.height}px`,
                transition: `color ${this.transitionTime}s`
            }
        },
        switchActionStyles() {
            return {
                width: `${this.width}px`,
                height: `${this.height}px`,
                borderRadius: `${this.height / 2}px`,
                background: this.activeBgColor,
                marginLeft: `${this.width * this.current}px`,
                transition: `margin-left ${this.transitionTime}s`,
                top: `${this.topBottom}px`,
                bottom: `${this.topBottom}px`,
                left: `${this.leftRight}px`
            }
        }
    },
    methods: {
        change(index) {
            if (this.current == index) return
            this.current = index
            this.$emit('change', this.list[index])
        }
    }
}
</script>

<style lang="scss" scoped>
    .switch{
        display: flex;
	   	align-items: center;
	   	justify-content: center;
        box-shadow: 0px 0px 50px 0px #e4e6f0 inset;
        position: relative;
        &__item{
            user-select: none;
   			cursor: pointer;
            display: flex;
		    align-items: center;
		    justify-content: space-between
            font-size: 16px;
            font-weight: 500;
            white-space: nowrap;
            position: absolute;
            z-index: 9;

            &__text{
                display: flex;
			   	align-items: center;
			   	justify-content: center;
                text-align: center;
            }
        }

        &__action{
            user-select: none;
   			cursor: pointer;
            pointer-events: none;
            position: absolute;
            z-index: 2;
        }
    }
</style>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DevilAngelia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值