vue3日历选择器

 倒叙日历:

<template>
  <div class="date-picker">
    <div class="column" @wheel="onYearScroll">
      <div v-for="(year, index) in displayedYears" :key="index" :class="{current: year === currentYear.value && index === 1}">
        {{ year }}
      </div>
    </div>
    <div class="column" @wheel="onMonthScroll">
      <div v-for="(month, index) in displayedMonths" :key="index" :class="{current: month === currentMonth.value && index === 1}">
        {{ monthString(month) }}
      </div>
    </div>
    <div class="column" @wheel="onDayScroll">
      <div v-for="(day, index) in displayedDays" :key="index" :class="{current: day === currentDay.value && index === 1}">
        {{ dayString(day) }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const currentYear = ref(new Date().getFullYear())
const currentMonth = ref(new Date().getMonth() + 1)
const currentDay = ref(new Date().getDate())

const displayedYears = computed(() => {
  const year = currentYear.value
  return [year + 1, year, year - 1, year - 2]
})

const displayedMonths = computed(() => {
  const month = currentMonth.value
  return [
    (month + 1 - 1) % 12 + 1,
    month,
    (month - 1 + 12) % 12 || 12,
    (month - 2 + 12) % 12 || 12,
  ]
})

const daysInMonth = (year, month) => {
  return new Date(year, month, 0).getDate()
}

const displayedDays = computed(() => {
  const year = currentYear.value
  const month = currentMonth.value
  const day = currentDay.value
  const daysInCurrentMonth = daysInMonth(year, month)
  return [
    (day + 1 - 1) % daysInCurrentMonth + 1,
    day,
    (day - 1 + daysInCurrentMonth - 1) % daysInCurrentMonth + 1,
    (day - 2 + daysInCurrentMonth - 1) % daysInCurrentMonth + 1
  ]
})

const onYearScroll = (event) => {
  event.preventDefault()
  if (event.deltaY < 0) {
    currentYear.value += 1
  } else {
    currentYear.value -= 1
  }
  // Reset month and day to 1
  currentMonth.value = 1
  currentDay.value = 1
}

const onMonthScroll = (event) => {
  event.preventDefault()
  if (event.deltaY < 0) {
    currentMonth.value = (currentMonth.value % 12) + 1
  } else {
    currentMonth.value = (currentMonth.value - 1 + 11) % 12 + 1
  }
  // Reset day to 1
  currentDay.value = 1
}

const onDayScroll = (event) => {
  event.preventDefault()
  const year = currentYear.value
  const month = currentMonth.value
  const daysInCurrentMonth = daysInMonth(year, month)
  if (event.deltaY < 0) {
    currentDay.value = (currentDay.value % daysInCurrentMonth) + 1
  } else {
    currentDay.value = (currentDay.value - 1 + daysInCurrentMonth - 1) % daysInCurrentMonth + 1
  }
}

const monthString = (month) => {
  return month.toString().padStart(2, '0')
}

const dayString = (day) => {
  return day.toString().padStart(2, '0')
}
</script>

<style>
.date-picker {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
  color: #fff !important;
}
.column {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 60px;
}
.column div {
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.current {
  font-weight: bold;
  color: red;
}
</style>

正序日历:

 

<template>
    <div class="date-picker">
      <div class="column" @wheel="onYearScroll">
        <div v-for="(year, index) in displayedYears" :key="index" :class="{current: year === currentYear}">
          {{ year }}
        </div>
      </div>
      <div class="column" @wheel="onMonthScroll">
        <div v-for="(month, index) in displayedMonths" :key="index" :class="{current: month === currentMonth}">
          {{ monthString(month) }}
        </div>
      </div>
      <div class="column" @wheel="onDayScroll">
        <div v-for="(day, index) in displayedDays" :key="index" :class="{current: day === currentDay}">
          {{ dayString(day) }}
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, computed } from 'vue'
  
  const currentYear = ref(new Date().getFullYear())
  const currentMonth = ref(new Date().getMonth() + 1)
  const currentDay = ref(new Date().getDate())
  
  const displayedYears = computed(() => {
    const year = currentYear.value
    return [year - 2, year - 1, year, year + 1, year + 2]
  })
  
  const displayedMonths = computed(() => {
    const month = currentMonth.value
    return [
      (month - 2 + 12) % 12 || 12,
      (month - 1 + 12) % 12 || 12,
      month,
      (month + 1 - 1) % 12 + 1,
      (month + 2 - 1) % 12 + 1
    ]
  })
  
  const daysInMonth = (year, month) => {
    return new Date(year, month, 0).getDate()
  }
  
  const displayedDays = computed(() => {
    const year = currentYear.value
    const month = currentMonth.value
    const day = currentDay.value
    const daysInCurrentMonth = daysInMonth(year, month)
    return [
      (day - 2 + daysInCurrentMonth) % daysInCurrentMonth || daysInCurrentMonth,
      (day - 1 + daysInCurrentMonth) % daysInCurrentMonth || daysInCurrentMonth,
      day,
      (day + 1 - 1) % daysInCurrentMonth + 1,
      (day + 2 - 1) % daysInCurrentMonth + 1
    ]
  })
  
  const onYearScroll = (event) => {
    event.preventDefault()
    if (event.deltaY > 0) {
      currentYear.value += 1
    } else {
      currentYear.value -= 1
    }
    // Reset month and day to 1
    currentMonth.value = 1
    currentDay.value = 1
  }
  
  const onMonthScroll = (event) => {
    event.preventDefault()
    if (event.deltaY > 0) {
      currentMonth.value = (currentMonth.value % 12) + 1
    } else {
      currentMonth.value = (currentMonth.value - 1 + 11) % 12 + 1
    }
    // Reset day to 1
    currentDay.value = 1
  }
  
  const onDayScroll = (event) => {
    event.preventDefault()
    const year = currentYear.value
    const month = currentMonth.value
    const daysInCurrentMonth = daysInMonth(year, month)
    if (event.deltaY > 0) {
      currentDay.value = (currentDay.value % daysInCurrentMonth) + 1
    } else {
      currentDay.value = (currentDay.value - 1 + daysInCurrentMonth - 1) % daysInCurrentMonth + 1
    }
  }
  
  const monthString = (month) => {
    return month.toString().padStart(2, '0')
  }
  
  const dayString = (day) => {
    return day.toString().padStart(2, '0')
  }
  </script>
  
  <style>
  .date-picker {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
    color: #fff !important;
  }
  .column {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 60px;
  }
  .column div {
    height: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .current {
    font-weight: bold;
    color: red;
  }
  </style>
  

手写Vue3日期选择器可以通过以下步骤实现: 1. 创建一个名为DatePicker的Vue组件。 2. 在组件中定义一个data属性,包含所需的日期数据,例如当前日期、选择的开始日期和结束日期。 3. 在模板中创建日期选择器的界面,包括一个日历表格和选择按钮。 4. 在方法中实现选择日期的逻辑。可以通过按钮点击事件或者直接在日历表格中选择日期来触发选择逻辑。在选择日期后,更新开始日期和结束日期的值。 5. 在组件中定义一个computed属性,用于计算并返回选择的日期范围。 6. 在模板中使用v-model指令将选择的日期范围绑定到父组件的数据上。 以下是一个简单的Vue3日期选择器的示例代码: ``` <template> <div> <input type="text" v-model="selectedRange" readonly> <button @click="showCalendar">选择日期</button> <div v-if="showCalendar"> <!-- 日历表格 --> </div> </div> </template> <script> import { ref, reactive, computed } from 'vue'; export default { setup() { // 数据初始化 const selectedRange = ref(''); const showCalendar = ref(false); const startDate = ref(null); const endDate = ref(null); // 显示日历 const showCalendar = () => { showCalendar.value = true; }; // 选择日期 const selectDate = (date) => { if (!startDate.value) { startDate.value = date; } else if (!endDate.value) { if (date > startDate.value) { endDate.value = date; } else { endDate.value = startDate.value; startDate.value = date; } } else { startDate.value = date; endDate.value = null; } }; // 计算选择的日期范围 const selectedRange = computed(() => { if (startDate.value && endDate.value) { return `${startDate.value} - ${endDate.value}`; } else if (startDate.value) { return startDate.value; } else { return ''; } }); return { selectedRange, showCalendar, selectDate }; } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值