uni-app 滑块组件,类似iPhone字体大小调节组件

<template>
	<view class="levelContent">
		<view class="line"></view>
		<view class="circle-collection" @click="clickArea">
			<view class="circle" @touchstart="drawStart" @touchmove="drawMove" @touchend="drawEnd"  :class="item == value ? 'active':''" v-for="item in data" :key="item">
			</view>
		</view>
		<view class="text-collection">
			<view class="circleText" :class="item == value ? 'active':''" v-for="item in data" :key="item">{
   
   {item}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'Slide',
		props: {
			// 放大模型倍数,默认为1倍
			data: {
				type: Array,
				default: []
			},
			value: {
				type: String
			},
		},
		data() {
			return {
				screenWidth: '',
				startMove: false,
				moveResult:''
			}
		},
		mounted() {
			this.screenWidth = wx.getSystemInfoSync().windowWidth
		},
		methods: {
			clickArea(e) {
				var width = this.screenWidth - 42;
				var width2 = (this.screenWidth - 42 - this.data.length * 11) / (this.data.length - 1)
				var x = e.changedTouches[0].clientX
				var num = ''
				for(var i = 0;i<this.data.length;i++){
					if (x < i * width2 + 21 + (i+1) * 11 + width2 / 2) {
						num = this.data[i]
						break
					}
				}
				if(num == ''){
					num = this.data[this.data.length-1]
				}
				if(num == this.value){
					this.startMove = true
				}else{
					this.startMove = false
				}
				this.$emit('changeValue', num);
			},
			drawStart(e) {
				var width = this.screenWidth - 42;
				var width2 = (this.screenWidth - 42 - this.data.length * 11) / (this.data.length - 1)
				var touch = e.touches[0];
				var x = touch.clientX
				var num = ''
				for(var i = 0;i<this.data.length;i++){
					if (x < i * width2 + 21 + (i+1) * 11 + width2 / 2) {
						num = this.data[i]
						break
					}
				}
				if(num == ''){
					num = this.data[this.data.length-1]
				}
				if(num == this.value){
					this.startMove = true
				}else{
					this.startMove = false
				}
			},
			//触摸滑动
			drawMove(e) {
				var width = this.screenWidth - 42;
				var width2 = (this.screenWidth - 42 - this.data.length * 11) / (this.data.length - 1)
				var touch = e.touches[0];
				var x = touch.clientX
				if(this.startMove){
					var num = ''
					for(var i = 0;i<this.data.length;i++){
						if (x < i * width2 + 21 + (i+1) * 11 + width2 / 2) {
							num = this.data[i]
							break
						}
					}
					if(num == ''){
						num = this.data[this.data.length-1]
					}
					if(this.value != num){
						this.moveResult = num
						this.$emit('moveValue', this.moveResult);
						wx.vibrateShort();
					}
					
				}
			},
			drawEnd(e) {
				this.$emit('changeValue', this.moveResult);
			},
		}
	}
</script>

<style scoped lang="scss">
	.levelContent {

		.line {
			//width: 660rpx;
			height: 10rpx;
			background-color: #3C425F;
			margin-left: 47rpx;
			margin-right: 47rpx;
			margin-top: 55rpx;
		}

		.circle-collection {
			//width: 670rpx;
			height: 42rpx;
			margin-left: 42rpx;
			margin-right: 42rpx;
			margin-top: -25rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.circle {
				align-items: center;
				width: 22rpx;
				height: 22rpx;
				background-color: #3C425F;
				border-radius: 50%;

				&.active {
					width: 42rpx;
					height: 42rpx;
					background: #FEB88F;
				}
			}
		}

		.text-collection {
			width: 670rpx;
			height: 42rpx;
			margin-left: 42rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.circleText {
				text-align: center;
				width: 22rpx;
				color: #999999;
				font-size: 20rpx;

				&.active {
					width: 42rpx;
				}
			}
		}
	}
</style>

 1、组件有两个参数,data:滑块的下标,比如字体大小可以有1,2,3,4等。value:当前值

猜你喜欢

转载自blog.csdn.net/qq_35323539/article/details/126668065