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>

效果图

相关推荐
召钱熏6 分钟前
状态枚举正确≠渲染正确:一个语音按钮的状态机边界修复实录
android·前端
SkyWalking中文站7 分钟前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台
运维·前端·监控
cidy_987 分钟前
Dify 操作教程:工作流编排 & Chat 对话编排
前端·工作流引擎
tangdou36909865510 分钟前
AI真好玩系列-2分钟快速了解DeepAgents | Quick Guide to DeepAgents in 2 Minutes
前端·javascript·后端
张元清11 分钟前
React useIntersectionObserver Hook:懒加载与可见性检测(2026)
javascript·react.js
小四的小六12 分钟前
AI Agent效果评测实战——搭完Agent才是噩梦的开始
前端
梨子同志20 分钟前
JavaScript
前端
彭于晏爱编程21 分钟前
纯 JS + Node,一个下午手搓了能读懂公司代码的 AI 助手,老板以为我转行了
前端·javascript
Delicate1 小时前
前端路由扫盲篇:Hash 模式和 History 模式到底怎么选?
前端
妙码生花1 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十四):眨眼小人登录页制作
前端·javascript·ai编程