vue3 时间轴选择某个时间段

vue3时间轴选择某段时间

支持选择多段时间
在这里插入图片描述

页面

 <div v-for="(item, idx) in color">
            <span
              style="width: 10px; height: 10px;display: inline-block;border-radius: 50%;"
              :style="{ background:item }"
            ></span>
            <span style="color: #000">第{{ idx + 1 }}次</span>
            <el-time-picker
              v-model="ruleForm[idx]"
              is-range
              format="HH:mm"
              value-format="x"
              style="width: 190px"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </div>
          <time-ruler
            style="width: 100%"
            v-model="ruleForm"
          ></time-ruler>
import timeRuler from "./timeRuler.vue";
const color = ["#FFDA23","#95EFFF", "#37FFDD", "#2475FF", "#FF986B", "#C0FF6B", "#FFDA23", "#95EFFF", "#37FFDD", "#2475FF", "#FF986B", "#C0FF6B",];
let ruleForm = ref([])

组件

<template>
  <div class="time-ruler">
    <div class="line0"></div>
    <div v-for="(item, idx) in ruleData" class="time-name">
      <div class="line" v-if="idx < 23">
        <div class="line-border"></div>
        <div
          class="line-1"
          v-for="(v, index) in props.modelValue"
          :style="{
            background: getStyle(item, index, 'background'),
            backgroundSize: getStyle(item, index, 'backgroundSize'),
          }"
        ></div>
      </div>
      <div class="name" v-if="item % 2 == 0">{{ item }}:00</div>
    </div>
    <div class="time-name1">
      <div class="line0"><div class="line-1"></div></div>
    </div>
  </div>
</template>
<script setup>
const props = defineProps({
  modelValue: [String],
});
const color = [  "#FFDA23", "#95EFFF", "#37FFDD", "#2475FF", "#FF986B",  "#C0FF6B", "#FFDA23", "#95EFFF", "#37FFDD",  "#2475FF",  "#FF986B",  "#C0FF6B",];
const ruleData = [  "08",  "09",  "10",  "11","12",  "13",  "14",  "15",  "16",  "17",  "18",  "19",  "20",  "21",  "22",  "23",  "00",  "01",  "02",  "03",  "04",  "05",  "06",  "07",];

function getStyle(time, index, type) {
  let background = "";
  let backgroundSize = "";
  const modelValue = props.modelValue[index];
  if (!modelValue) {
    return;
  }
  const hoursStart = new Date(modelValue[0]).getHours();
  const hoursEnd = new Date(modelValue[1]).getHours();
  const minutesStart = new Date(modelValue[0]).getMinutes();
  const minutesEnd = new Date(modelValue[1]).getMinutes();
  if (hoursStart <= time * 1 && hoursEnd > time * 1) {
    if (hoursStart === time * 1 && minutesStart > 0) {
      const percentage = 100 - (minutesStart / 60) * 100;
      backgroundSize = `${percentage}% 100%`;
      background = `linear-gradient(${color[index]},${color[index]}) right no-repeat`;
    } else {
      background = color[index];
    }
  }
  if (time - hoursEnd === 0 && minutesEnd !== 0) {
    const percentage = (minutesEnd / 60) * 100;
    backgroundSize = `${percentage}% 100%`;
    background = `linear-gradient(${color[index]},${color[index]}) left no-repeat`;
  }

  if (time - hoursEnd === 0 && minutesEnd === 0) {
    backgroundSize = `0`;
    background = `linear-gradient(${color[index]},${color[index]}) left no-repeat`;
  }
  if (type === "background") {
    return background;
  } else {
    return backgroundSize;
  }
}
</script>

<style lang="scss" scoped>
.time-ruler {
  width: 100%;
  height: 50px;
  display: flex;
  flex-direction: row;
  font-size: 10px;
  .time-name {
    position: relative;
    .name {
      position: absolute;
      bottom: -8px;
      left: -15px;
      color: #000;
    }
  }
  .line {
    width: 30px;
    height: 26px;
    margin-bottom: 10px;
    border-bottom: 1px solid #dbdbdb;
    position: relative;
    .line-border {
      border-left: 1px solid #dbdbdb;
      height: 100%;
      z-index: 11;
      position: relative;
    }
    .line-1 {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      opacity: 0.65;
    }
  }

  .line0 {
    width: 20px;
    height: 26px;
    margin-bottom: 5px;
    border-bottom: 1px solid #dbdbdb;
    .line-1 {
      border-left: 1px solid #dbdbdb;
      height: 100%;
    }
  }
}
</style>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sbb0_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值