subsection.vue 2.52 KB
<template>
	<div class="u-subsection" ref="container">
		<div class="u-subsection-item" :class="{ active: index + 1 === value }" v-for="(item, index) in list" :key="index" @mousedown="handlePressStart(index + 1)" @mouseup="handlePressEnd" @mouseleave="handlePressEnd" @touchstart.prevent="handlePressStart(index + 1)" @touchend="handlePressEnd" @touchcancel="handlePressEnd">
			{{ item.label }}
		</div>
		<!-- 滑动高亮条 - 保持不变 -->
		<div
			class="slider"
			:class="'slider-position-' + value"
			:style="{
				background: value === 1 ? '#ffd700' : '#32b92b',
			}"
		></div>
	</div>
</template>

<script>
export default {
	name: 'USubsection',
	props: {
		list: {
			type: Array,
			default: () => [],
		},
		value: {
			type: Number,
			default: 1,
		},
	},
	data() {
		return {
			pressTimer: null,
			pressDurationMs: 500,
		}
	},
	methods: {
		handlePressStart(val) {
			this.clearPressTimer()
			this.pressTimer = setTimeout(() => {
				this.handleLongPressConfirm(val)
			}, this.pressDurationMs)
		},
		handlePressEnd() {
			this.clearPressTimer()
		},
		handleLongPressConfirm(val) {
			this.clearPressTimer()
			if (val === this.value) {
				return
			}
			this.$emit('input', val) // 保持v-model原有逻辑
			this.$emit('tab-change', val) // 新增:派发自定义切换事件,携带选中值
		},
		clearPressTimer() {
			if (this.pressTimer) {
				clearTimeout(this.pressTimer)
				this.pressTimer = null
			}
		},
	},
	beforeDestroy() {
		this.clearPressTimer()
	},
}
</script>

<style scoped>
.u-subsection {
	width: 16vw;
	height: 3vw;
	display: flex;
	position: relative;
	background: #e0e0e0;
	border-radius: 0.6vw;
	margin: 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 {
	flex: 1;
	padding: 0.5vw 0;
	cursor: pointer;
	font-size: 0.9vw;
	color: #333;
	position: relative;
	z-index: 2;
	display: flex;
	align-items: center;
	justify-content: center;
	text-align: center;
	user-select: none;
}

.u-subsection-item.active {
	color: black;
	/* text-shadow: 0.1vw 0.1vw 0.2vw rgba(0, 0, 0, 0.3); */
}

.slider {
	width: 6.6vw;
	height: 2.2vw;
	position: absolute;
	top: 0.3vw;
	border-radius: 0.4vw;
	z-index: 1;
	box-shadow: 0 0.1vw 0.3vw rgba(0, 0, 0, 0.2);
	transition: all 0.3s ease;
}

/* 固定位置版本 */
.slider-position-1 {
	left: 0.5vw;
	width: 6.8vw;
	background: #ffd700 !important; /* 强制黄色 */
}

.slider-position-2 {
	left: 8.6vw;
	width: 6.8vw;
	background: #32b92b !important; /* 强制绿色 */
}
</style>