javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > uniapp滚动选择器

uniapp手写滚动选择器的完整代码(时间选择器)

作者:奶糖 肥晨

这篇文章主要介绍了uniapp手写滚动选择器的完整代码(时间选择器),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

没有符合项目要求的选择器 就手写了一个

效果展示

在这里插入图片描述

实现一个时间选择器的功能,可以选择小时和分钟:

HTML/Template部分:

<picker-view
  class="sleepPage-time-picker"
  :indicator-style="indicatorStyle"
  :value="timeValue"
  @change="handleTimeChange"
>
  <!-- 第一列:小时选择 -->
  <picker-view-column>
    <view
      v-for="(hour, index) in hours"
      :key="index"
      :class="[
        'sleepPage-time-picker_item',
        { selected: timeValue[0] === index },
      ]"
    >
      {{ hour }}
      <span
        class="sleepPage-time-picker_item-span"
        v-if="timeValue[0] === index"
        >时</span
      >
    </view>
  </picker-view-column>
  <!-- 第二列:分钟选择 -->
  <picker-view-column>
    <view
      v-for="(minute, index) in minutes"
      :key="index"
      :class="[
        'sleepPage-time-picker_item',
        { selected: timeValue[1] === index },
      ]"
    >
      {{ minute }}
      <span
        class="sleepPage-time-picker_item-span"
        v-if="timeValue[1] === index"
        >分</span
      >
    </view>
  </picker-view-column>
</picker-view>

JavaScript部分:

data() {
  return {
    timeValue: [0, 0],  // 默认选中的时间值,[小时索引, 分钟索引]
    indicatorStyle: "height: 30px;background: rgba(237, 252, 249, 1);z-index: 0;",
    hours: [...Array(24).keys()].map((n) => n.toString().padStart(2, "0")),  // 生成小时选项数组
    minutes: [...Array(60).keys()].map((n) => n.toString().padStart(2, "0")),  // 生成分钟选项数组
  };
},
methods: {
  handleTimeChange(e) {
    this.timeValue = e.detail.value;  // 更新选择的时间值
    // 处理选择后的逻辑,例如更新界面显示的时间
    console.log(
      "Selected Time:",
      this.hours[this.timeValue[0]],
      ":",
      this.minutes[this.timeValue[1]]
    );
  },
}

CSS部分:

.sleepPage-time-picker-box {
  display: flex;
  margin-bottom: 10px;
}
.sleepPage-time-picker {
  height: 90px;  /* 设置选择器的高度 */
  width: 50%;  /* 设置选择器的宽度 */
  margin: 2px;
}
.selected {
  color: rgba(40, 184, 129, 1);  /* 设置选中项的文字颜色 */
}
.sleepPage-time-picker_item {
  text-align: center;
  height: 30px;
  line-height: 30px;
  position: relative;
}
.sleepPage-time-picker_item-span {
  padding-left: 10px;
  position: absolute;
  right: 15px;
}

CSS 部分定义了选择器和其子元素的样式,包括选择器的整体布局和每个选项的样式,以及选中项的特殊样式。

完整代码

     <picker-view
          class="sleepPage-time-picker"
          :indicator-style="indicatorStyle"
          :value="timeValue"
          @change="handleTimeChange"
        >
          <picker-view-column>
            <view
              v-for="(hour, index) in hours"
              :key="index"
              :class="[
                'sleepPage-time-picker_item',
                { selected: timeValue[0] === index },
              ]"
            >
              {{ hour }}
              <span
                class="sleepPage-time-picker_item-span"
                v-if="timeValue[0] === index"
                >时</span
              >
            </view>
          </picker-view-column>
          <picker-view-column>
            <view
              v-for="(minute, index) in minutes"
              :key="index"
              :class="[
                'sleepPage-time-picker_item',
                { selected: timeValue[1] === index },
              ]"
            >
              {{ minute }}
              <span
                class="sleepPage-time-picker_item-span"
                v-if="timeValue[1] === index"
                >分</span
              >
            </view>
          </picker-view-column>
        </picker-view>
      timeValue: [0, 0],
      indicatorStyle:
        "height: 30px;background: rgba(237, 252, 249, 1);z-index: 0;",
      hours: [...Array(24).keys()].map((n) => n.toString().padStart(2, "0")),
      minutes: [...Array(60).keys()].map((n) => n.toString().padStart(2, "0")),
    handleTimeChange(e) {
      this.timeValue = e.detail.value;
      // 这里可以处理时间选择后的逻辑,例如更新界面显示的时间
      console.log(
        "Selected Time:",
        this.hours[this.timeValue[0]],
        ":",
        this.minutes[this.timeValue[1]]
      );
    },
  .sleepPage-time-picker-box {
    display: flex;
	margin-bottom: 10px;
    .sleepPage-time-picker {
      // height: 300px;
      height: 90px;
      width: 50%;
      margin: 2px;
    }
    .selected {
      color: rgba(40, 184, 129, 1);
    }
    .sleepPage-time-picker_item {
      text-align: center;
      height: 30px;
      line-height: 30px;
      position: relative;
    }
    .sleepPage-time-picker_item-span {
      padding-left: 10px;
      position: absolute;
      right: 15px;
    }
  }

您好,我是肥晨。
欢迎关注我获取前端学习资源,日常分享技术变革,生存法则;行业内幕,洞察先机。

到此这篇关于uniapp手写滚动选择器的文章就介绍到这了,更多相关uniapp滚动选择器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文