subsection.vue 1.91 KB
<template>
	<div class="u-subsection">
		<div class="u-subsection-item" :class="{ active: index + 1 === value }" v-for="(item, index) in list" :key="index" @click="handleClick(index + 1)" ref="tabItems">
			{{ item.label }}
		</div>

		<!-- 滑动高亮条 -->
		<div class="slider" :style="sliderStyle"></div>
	</div>
</template>

<script>
export default {
	name: 'USubsection',
	props: {
		list: {
			type: Array,
			default: () => [],
		},
		value: {
			type: Number,
			default: 1,
		},
	},
	data() {
		return {
			sliderStyle: {
				left: '0px',
				width: '0px',
			},
		}
	},
	mounted() {
		// 初始化位置
		this.$nextTick(() => {
			this.setSliderPosition(this.value - 1)
		})
	},
	watch: {
		// 监听选中变化,自动滑动
		value: {
			handler(newVal) {
				this.$nextTick(() => {
					this.setSliderPosition(newVal - 1)
				})
			},
		},
	},
	methods: {
		handleClick(val) {
			this.$emit('input', val)
		},
		// 设置滑动条位置和宽度
		setSliderPosition(index) {
			if (!this.$refs.tabItems || !this.$refs.tabItems[index]) return

			const item = this.$refs.tabItems[index]
			this.sliderStyle.left = item.offsetLeft + 'px'
			this.sliderStyle.width = item.offsetWidth + 'px'
		},
	},
}
</script>

<style scoped>
.u-subsection {
	width: 16vw;
	height: 3vw;
	display: flex;
	position: relative;
	gap: 2vw;
	padding: 0.2vw 0.3vw;
	background: #e0e0e0;
	border-radius: 0.6vw;
	margin: 0vw 0;
	overflow: hidden;
	border: 0.12vw solid #3f3e3e;
	box-shadow: 0.2vw 0.2vw 0.2vw #333, 0.3vw 0.3vw 1vw rgba(0, 0, 0, 0.3);
}

.u-subsection-item {
	padding: 0.5vw 1.29vw;
	border-radius: 0.4vw;
	cursor: pointer;
	font-size: 1vw;
	color: #333;
	position: relative;
	z-index: 2;
	transition: color 0.2s ease;
}

.u-subsection-item.active {
	color: #fff;
}

.slider {
	position: absolute;
	top: 0.6vw;
	bottom: 0.6vw;
	background: #409eff;
	border-radius: 0.4vw;
	transition: left 0.25s linear, width 0.2s ease;
	z-index: 1;
}
</style>