vue3滚动日历选择器

news2024/10/7 12:29:05

 倒叙日历:

<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>
  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1859136.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

只知道无人机能航拍,你已经out啦!!!

无人机行业应用相当广泛&#xff0c;涵盖了航拍、植保、测绘、巡检、安防、物流等多个领域。并且随着科技的不断发展&#xff0c;无人机技术也在不断创新和完善。无人机行业的应用具有多重优势&#xff0c;这些优势使得无人机在众多领域中得以广泛应用并取得显著的成效。 航拍…

电脑不小心删除的文件怎么恢复?4个必备恢复方法!

“刚刚在对电脑里的某些垃圾文件进行清理时&#xff0c;我一不小心误删了比较重要的数据。这些误删的数据还有机会恢复吗&#xff1f;希望大家帮帮我&#xff0c;非常感谢&#xff01;” 在这个数字化飞速发展的时代&#xff0c;电脑早已成为我们日常生活和工作中不可或缺的一部…

AI如何让办公更智能?WPS AI海外版给出答案

导读&#xff1a;从语义检查到一键生成PPT&#xff0c;WPS Office海外版如何面向2亿月活用户快速推出AI功能&#xff1f; 近日&#xff0c;WPS Office海外版应用亚马逊云科技Amazon Bedrock等生成式AI技术与服务&#xff0c;在海外正式推出人工智能应用WPS AI海外版&#xff0c…

扣子/coze智能体开发的经验与避坑指南

近期&#xff0c;我计划几场关于分享智能体应用开发的活动。因此&#xff0c;我顺便总结了我在创建智能体过程中遇到的问题和解决方案&#xff0c;帮助大家避免类似的陷阱&#xff0c;提高智能体的性能和用户体验。以下是我总结的几点关键经验。 1. 人设与回复逻辑的提示词 在…

提取图像主色调

依赖 Pillow 库。 提取图像主色调&#xff0c;直接上代码&#xff1a; from PIL import Imagedef extract_main_color(img_path: str, delta_h: float 0.3) -> str:"""获取图像主色调Args:img_path: 输入图像的路径delta_h: 像素色相和平均色相做减法的绝…

react学习——14react生命周期图(旧)

1、生命周期图 2、单个组件 class Demo extends React.Component{//构造器constructor(props){console.log("count--constructor")super(props)this.state{count: 1}}//组件将要挂载componentWillMount(){console.log("count--componentWillMount")}//组件…

2024年计算机专业还值得选吗?

个人认为可以 一、就业前景广阔 市场需求旺盛&#xff1a;随着数字化和信息化的快速发展&#xff0c;计算机技术已经渗透到各个行业和领域。无论是传统制造业、金融、医疗&#xff0c;还是新兴的互联网、人工智能等领域&#xff0c;都离不开计算机专业人才的支持。因此&#x…

FVCOM水环境、污染物迁移、水交换、水质、潮流、温盐、波浪及泥沙数值模拟

近年来&#xff0c;随着计算技术的发展和对海洋、水环境问题认识的加深&#xff0c;数值模拟技术在海洋、水环境等科学研究中的应用越来越广泛。FVCOM因其独特的优点&#xff0c;成为研究海洋动力过程、污染物扩散、水质变化等问题的重要工具。作为一种基于有限体积法的数值模型…

Trilium Notes浏览器插件保存网页内容到docker私有化部署

利用Trilium浏览器插件可以很方便的把网页内容保存到Trilium&#xff0c;需要先在docker部署好trilium&#xff0c;还没有部署的可以先看这篇文章&#xff1a;trilium笔记私有化部署-www.88531.cn资享网 1.下载Trilium浏览器插件&#xff1a;https://www.npspro.cn/33462.html…

第23篇 滑动开关控制LED<一>

Q&#xff1a;如何使用Intel FPGA Monitor Program设计实现滑动开关控制LED的汇编程序呢&#xff1f; A&#xff1a;基本原理&#xff1a;该应用程序用到DE2-115开发板上的18个红色LED和18个滑动开关SW&#xff0c;DE2-115_Computer system的qsys系统中IP的硬件信息如模块类型…

pyppeteer模块经常使用的功能,相关操作案例

官方仓库地址&#xff1a;https://github.com/miyakogi/pyppeteer 官方文档地址&#xff1a;API Reference — Pyppeteer 0.0.25 documentation Selenium环境的相关配置比较繁琐&#xff0c;此外&#xff0c;有的网站会对selenium和webdriver进行识别和反爬&#xff0c;因此在…

高中数学:数列-解数列不等式问题的常用放缩技巧(重难点)

一、放缩技巧 技巧1 例题 证明&#xff1a;Sn&#xff1c;1 解&#xff1a; 变形 解&#xff1a; 由于第一种情况&#xff0c;我们证明了Sn&#xff1c;1&#xff0c;n≥1&#xff0c;是从第一项就开始放缩的。 发现&#xff0c;无法精确到 3 4 \frac{3}{4} 43​ 这时&am…

Behind the Code:Polkadot 如何实现全球协作与去中心化治理?

2024 年 6 月 16 日&#xff0c;《Behind the Code: Web3 Thinkers》第二季第二集上线。本集中&#xff0c;ChaosDAO 联合创始人兼 Novasama Technologies 首席财务官 Leemo 深入探讨了 Polkadot 生态系统中的全球协作力量&#xff0c;以及这种协作如何推动去中心化治理的创新与…

管理后台

自学python如何成为大佬(目录):https://blog.csdn.net/weixin_67859959/article/details/139049996?spm1001.2014.3001.5501 定义好数据模型&#xff0c;就可以配置管理后台了&#xff0c;按照如下代码编辑app1下面的admin.py文件&#xff1a; from django.contrib import a…

我也认为说 360 无法卸载这一说法,是一个 “彻头彻尾的谣言”

最近&#xff0c;360 公司董事长周鸿祎发布视频回应了 360 无法卸载这一说法&#xff0c;称其是一个 “彻头彻尾的谣言”。他解释道&#xff0c;360 软件完全可以卸载&#xff0c;在设置里面有卸载的入口&#xff0c;通过软件管家也可以正常卸载。不能卸载的说法完全是断章取义…

嵌入式实验---实验七 SPI通信实验

一、实验目的 1、掌握STM32F103SPI通信程序设计流程&#xff1b; 2、熟悉STM32固件库的基本使用。 二、实验原理 1、使用STM32F103R6通过74HC595控制一位LID数码管&#xff0c;实现以下两个要求&#xff1a; &#xff08;1&#xff09;数码管从0到9循环显示&#xff1b; …

17.RedHat认证-Ansible自动化运维(下)

17.RedHat认证-Ansible自动化运维(下) 这个章节讲ansible的变量&#xff0c;包括变量的定义、变量的规则、变量范围、变量优先级、变量练习等。 以及对于tasks的控制&#xff0c;主要有loop循环作业、条件判断等 变量 介绍 Ansible支持变量功能&#xff0c;能将value存储到…

快速排序的实现(3种)

目录 0.快速排序1.Hoare版本1.1基本思想1.2算法描述1.3画图解释1.4问题&#xff1f;1.5代码实现 2.挖坑法2.1算法描述2.2画图解释2.3代码实现 3.先后指针法3.1算法描述3.2画图解释3.3代码实现 4.优化4.1优化方法4.2优化代码 5.非递归实现快排5.1算法描述 0.快速排序 1.时间复杂…

计算机系统基础知识(上)

目录 计算机系统的概述 计算机的硬件 处理器 存储器 总线 接口 外部设备 计算机的软件 操作系统 数据库 文件系统 计算机系统的概述 如图所示计算机系统分为软件和硬件&#xff1a;硬件包括&#xff1a;输入输出设备、存储器&#xff0c;处理器 软件则包括系统软件和…

代码随想录算法训练营第四十一天| 416. 分割等和子集

416. 分割等和子集 - 力扣&#xff08;LeetCode&#xff09; class Solution {public boolean canPartition(int[] nums) {int sum 0;for (int i0;i<nums.length;i){sum nums[i];}if(sum%2!0){return false;}int weight sum /2;// int[][] dp new int[nums.length][weig…