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:当前值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值