# 该案例的实现思路

每个盒子都有自己的索引值,在你选中一个盒子之后,会在数组 a 中记录这个索引值,而这时我们需要定义一个盒子移入就触发的事件,在选中一个盒子的条件下(也就是数组 a 的 length 为 1 时)将你移入的那个盒子的索引值记录到数组 b 中,这时数组 a 和数组 b 中都有一个索引值,我们只需要将在那些索引值在这两个索引值之间的盒子高亮一个颜色就可以了,而数组 a 里面的索引值所对应的盒子设置为另一个高亮颜色,就可实现

# 案例演示

image-20220506155720417

在点击某个格子后会高亮一个盒子,然后可以在盒子范围内选中第二个盒子即可获取选中区域

image-20220506155733632

image-20220506155742316

# 实现代码

<template>
  <div>
    <div class="VueTimeSlot">
      <div class="VueTimeSlotList">
        <div
          class="VueTimeSlotItems"
          v-for="(item, index) in times"
          :key="index"
        >
          {{ getItemsSection(index) }}
          <div
            class="VueTimeSlotBox"
            :class="{
              VueTimeSlotBoxSelect: BlueBox.includes(index),
              VueTimeSlotBoxItems: getItemsSection(index)
            }"
            @mouseover="ItmeListOnMouseover(index)"
            @click="ItmeListOnclick(index)"
          ></div>
          {{ index }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
const times = ref<number[]>([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

const BlueBox = ref<number[]>([])
const TimeItems = ref<number[]>([])
const ItmeListOnclick = (id: number) => {
  BlueBox.value.push(id)
  if (BlueBox.value.length > 2) {
    BlueBox.value = []
    TimeItems.value = []
    BlueBox.value.push(id)
  }
}

const ItmeListOnMouseover = (id: number) => {
  if (BlueBox.value.length == 1) {
    TimeItems.value[0] = id
  }
}

const getItemsSection = (id: number) => {
  let flag = false
  if (id > BlueBox.value[0]) {
    flag = id > BlueBox.value[0] && id < TimeItems.value[0]
  } else {
    flag = id < BlueBox.value[0] && id > TimeItems.value[0]
  }
  return flag
}
</script>

<style lang="scss" scoped>
.VueTimeSlot {
  .VueTimeSlotList {
    display: flex;
    .VueTimeSlotItems {
      // background-color: red;
      display: inline-block;
      padding: 30px 0px;
      &:last-child {
        .VueTimeSlotBox {
          border-right: 1px #ccc solid;
        }
      }
      .VueTimeSlotBox {
        width: 50px;
        height: 20px;
        border: 1px #ccc solid;
        border-right: none;
        cursor: pointer;
      }
      .VueTimeSlotBoxSelect {
        background-color: rgb(50, 150, 250) !important;
      }
      .VueTimeSlotBoxItems {
        background-color: rgba(50, 150, 250, 0.3);
      }
    }
  }
}
</style>