vue3横向滚动日期选择器组件(Element Plus)

vue横向滚动日期选择器组件

组件使用到了element-plus组件库和dayjs库,使用前先保证项目中已经下载导入

主要功能:选择日期,点击日期可以让此日期滚动到视图中间,左滑右滑同理,支持跳转至任意日期,支持自定义滚动日期的数量

组件中用到了other.ts
工具代码other.ts

import dayjs from 'dayjs'
import calendar from 'dayjs/plugin/calendar'
import 'dayjs/locale/zh-cn'

dayjs.locale('zh-cn')
dayjs.extend(calendar)
dayjs().calendar(null, {
   
   
  sameDay: '[Today]', // The same day ( Today at 2:30 AM )
  nextDay: '[Tomorrow]', // The next day ( Tomorrow at 2:30 AM )
  nextWeek: 'dddd', // The next week ( Sunday at 2:30 AM )
  lastDay: '[Yesterday]', // The day before ( Yesterday at 2:30 AM )
  lastWeek: '[Last]', // Last week ( Last Monday at 2:30 AM )
  sameElse: 'DD/MM/YYYY' // Everything else ( 7/10/2011 )
})

function judegSame(dj1: dayjs.Dayjs, dj2: dayjs.Dayjs) {
   
   
  return dj1.isSame(dj2)
}

function getRelativeTime(dj: dayjs.Dayjs) {
   
   
  let now = dayjs(dayjs().format('YYYY-MM-DD'))
  if (judegSame(now, dj)) {
   
   
    return '今天'
  }
  now = now.add(1, 'day')
  if (judegSame(now, dj)) {
   
   
    return '明天'
  }
  now = now.add(1, 'day')
  if (judegSame(now, dj)) {
   
   
    return '后天'
  }
  let d = dj.day()
  const backArr = ['日', '一', '二', '三', '四', '五', '六']
  return '周' + backArr[d]
}

export {
   
    dayjs, getRelativeTime }

组件代码SlideDatePicker.vue

<script setup lang="ts">
import {
     
      ArrowLeft, ArrowRight } from '@element-plus/icons-vue';
import {
     
      dayjs, getRelativeTime } from './other';

// 日期加载总量
const {
     
      count } = withDefaults(defineProps<{
     
     
  count: number
}>(), {
     
     
  count: 30
})
const activeIndex = ref(0)
const dateItemRefs = ref<HTMLElement[]>([])
const dateItmeWrapRef = ref<HTMLElement>()
const curDate = ref('') // 日期选择器 选择的日期

const showDateList = ref<Record<string, any>[]>([])

const emit = defineEmits(['dateChange'])

function calc(format?: string) {
     
     
  if (!format) {
     
     
    format = dayjs().format('YYYY-MM-DD')
  }
  showDateList.value = []
  let beforeCount = Math.floor((count + 1) / 2) // 上取整
  let afterCount = Math.floor(count / 2)
  let cur = dayjs(dayjs(format).format('YYY
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你熬夜了吗?

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

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

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

打赏作者

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

抵扣说明:

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

余额充值