uniapp手写滚动选择器

代码

复制代码
<template>
	<view class="container">
		<picker-view class="sleepPage-time-picker" :indicator-style="indicatorStyle" :value="timeValue"
			@change="handleTimeChange">
			<picker-view-column>
				<view v-for="(val, index) in subsections" :key="index"
					:class="['sleepPage-time-picker_item',{ selected: timeValue[0] === index }]">
					{{ val }}
				</view>
			</picker-view-column>
			<!-- 小时选择 -->
			<picker-view-column>
				<view v-for="(hour, index) in hours" :key="index"
					:class="['sleepPage-time-picker_item',{ selected: timeValue[1] === index }]">
					{{ hour }}
				</view>
			</picker-view-column>
			<!-- 分钟选择 -->
			<picker-view-column>
				<view v-for="(minute, index) in minutes" :key="index"
					:class="['sleepPage-time-picker_item',{ selected: timeValue[2] === index }]">
					{{ minute }}
				</view>
			</picker-view-column>
		</picker-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timeValue: [0, 0, 0], // 默认选中的时间值,[小时索引, 分钟索引]
				indicatorStyle: "height: 50px;background: #fff;z-index: 0;",
				hours: [...Array(12).keys()].map((n) => (n + 1).toString().padStart(2, "0")), // 生成小时选项数组
				minutes: [...Array(60).keys()].map((n) => n.toString().padStart(2, "0")), // 生成分钟选项数组
				subsections: ["上午", "下午"], // 生成分钟选项数组
			};
		},
		onLoad() {
			let date = new Date();
			let hours = date.getHours();
			let minutes = date.getMinutes();
			console.log("hours--", hours);
			console.log("minutes--", minutes);
			let subsections = hours < 12 ? 0 : 1
			if (hours == 0) {
				hours = 12
			} else if (hours < 12) {
				hours = hours - 1
			} else if (hours > 12) {
				hours = hours - 13
			}
			this.timeValue = [subsections, hours, minutes]
		},
		methods: {
			handleTimeChange(e) {
				console.log("e.detail---", e.detail);
				this.timeValue = e.detail.value; // 更新选择的时间值
				// 处理选择后的逻辑,例如更新界面显示的时间
				console.log(
					"Selected Time:",
					this.hours[this.timeValue[0]],
					":",
					this.minutes[this.timeValue[1]]
				);
			},
		}

	}
</script>

<style>
	.container {}

	.sleepPage-time-picker-box {
		display: flex;
		margin-bottom: 10px;
	}

	.sleepPage-time-picker {
		height: 380rpx;
		width: 500rpx;
		margin: 0 auto;
	}

	.selected {
		color: #29c9d0;
	}

	.sleepPage-time-picker_item {
		text-align: center;
		height: 50px;
		line-height: 50px;
		position: relative;
		font-size: 40rpx;
	}
</style>

效果图

相关推荐
梅羽落2 小时前
python武器化开发_01
开发语言·python·php
Joe_Blue_022 小时前
Matlab 入门案例介绍——如何创建脚本
开发语言·matlab·matlab 入门案例
nwsuaf_huasir2 小时前
积分旁瓣电平-matlab函数
前端·javascript·matlab
崇山峻岭之间2 小时前
Matlab学习记录20
开发语言·学习·matlab
韭菜炒大葱2 小时前
React Hooks :useRef、useState 与受控/非受控组件全解析
前端·react.js·前端框架
逍遥德2 小时前
JPA 操作对象图 (Object Graph) 详解
开发语言·python
Cache技术分享2 小时前
280. Java Stream API - Debugging Streams:如何调试 Java 流处理过程?
前端·后端
微爱帮监所写信寄信2 小时前
微爱帮监狱寄信写信小程序信件内容实时保存技术方案
java·服务器·开发语言·前端·小程序
沛沛老爹2 小时前
Web开发者实战A2A智能体交互协议:从Web API到AI Agent通信新范式
java·前端·人工智能·云原生·aigc·交互·发展趋势
李少兄2 小时前
时间戳转换工具
开发语言·javascript·工具