• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

VUE 3 TS 自定义 Calendar 日历 可以点击选择 和 滑动选择日期 夸月选择

武飞扬头像
民工_
帮助1

最近项目中需要一个可以自由选择日期的空间,在网上找了很久都没有适合的可用,索性自己写了一个,看代码。 可以滑动选择和点击选择  竖向点击星期几选择

学新通

  1.  
    <template>
  2.  
    <el-button type="primary" @click="ClickReset" >重置</el-button>
  3.  
    <el-button type="primary" @click="isShow=!isShow" >显示价格</el-button>
  4.  
    <div class="flex-wrap text-primary" @mouseup="Mouseup">
  5.  
    <div v-for="month in monthlist" :key="month" class="calendar-month mt-24 ml-16">
  6.  
    <div class="pb-8 text-14 text-center">{{ month.date }}</div>
  7.  
    <ul class="flex-wrap text-sub">
  8.  
    <li class="text-tip" @click="ClickWeek(month,0)"></li>
  9.  
    <li class="text-tip" @click="ClickWeek(month,1)"></li>
  10.  
    <li class="text-tip" @click="ClickWeek(month,2)"></li>
  11.  
    <li class="text-tip" @click="ClickWeek(month,3)"></li>
  12.  
    <li class="text-tip" @click="ClickWeek(month,4)"></li>
  13.  
    <li class="text-tip" @click="ClickWeek(month,5)"></li>
  14.  
    <li class="text-tip" @click="ClickWeek(month,6)"></li>
  15.  
     
  16.  
    <li v-for="item in month.week" :key="item" data-id="">
  17.  
    <div></div>
  18.  
    </li>
  19.  
     
  20.  
    <li v-for="item in month.days" :key="item" :data-id="item.date"
  21.  
    @click="ClickItem(item)"
  22.  
    @mousedown="Mousedown(item)"
  23.  
    @mouSEO((Search Engine Optimization))ver="MouSEO((Search Engine Optimization))ver(item)"
  24.  
    @mouseup="Mouseup">
  25.  
    <div class="relative" :class="{'isHover':item.hover,'isSelected':item.selected}" >
  26.  
    <p class="">{{ item.day }}</p>
  27.  
    <span v-show="isShow" class="absolute color-red">{{item.price}}</span>
  28.  
    </div>
  29.  
    </li>
  30.  
     
  31.  
    </ul>
  32.  
    </div>
  33.  
    </div>
  34.  
    </template>
  35.  
     
  36.  
    <script lang="ts" setup>
  37.  
     
  38.  
    import {onMounted, ref} from "vue";
  39.  
    import moment from "moment";
  40.  
     
  41.  
     
  42.  
    interface Month {
  43.  
    date: string,
  44.  
    days:Day[],
  45.  
    week:number,
  46.  
    }
  47.  
     
  48.  
    interface Day {
  49.  
    date: string,
  50.  
    week:number,
  51.  
    day: string,
  52.  
    hover: boolean,
  53.  
    selected: boolean,
  54.  
    price:string,
  55.  
    }
  56.  
     
  57.  
    const isShow= ref(false);
  58.  
    const monthlist=ref<Month[]>([]);
  59.  
     
  60.  
    onMounted(()=>{
  61.  
     
  62.  
    console.log('onMounted')
  63.  
     
  64.  
    for (let i = 0; i < 6; i ) {
  65.  
    let firstdate = moment().add(i,'month').startOf('month');
  66.  
    let enddate = moment().add(i,'month').endOf('month');
  67.  
    let date = firstdate.toDate();
  68.  
    let days:Day[] = [];
  69.  
    for (let j = 0; j < enddate.diff(date,'days') 1; j ) {
  70.  
    let date1 = moment(date).add(j,'day').toDate();
  71.  
    days.push({
  72.  
    date: moment(date1).format('YYYY-MM-DD'),
  73.  
    week:moment(date1).day(),
  74.  
    day: moment(date1).date() '',
  75.  
    hover:false,
  76.  
    selected: false,
  77.  
    price:'¥2000'
  78.  
    })
  79.  
    }
  80.  
     
  81.  
    monthlist.value.push({
  82.  
    date: firstdate.format('YYYY 年 MM 月'),
  83.  
    days:days,
  84.  
    week:firstdate.day(),
  85.  
    }
  86.  
    );
  87.  
    }
  88.  
     
  89.  
    console.log(monthlist);
  90.  
     
  91.  
    })
  92.  
     
  93.  
    const ClickItem = (day:Day) => {
  94.  
    day.selected=!day.selected;
  95.  
    }
  96.  
     
  97.  
    let date_begin = ''
  98.  
    const Mousedown = (day:Day) => {
  99.  
    date_begin=day.date;
  100.  
    }
  101.  
     
  102.  
    const MouSEO((Search Engine Optimization))ver = (day:Day) => {
  103.  
    if(date_begin!='')
  104.  
    {
  105.  
    for (let i = 0; i < monthlist.value.length; i ) {
  106.  
    for (let j = 0; j < monthlist.value[i].days.length; j ) {
  107.  
    let day1 = monthlist.value[i].days[j];
  108.  
    if((day1.date >= day.date && day1.date <= date_begin)||(day1.date <= day.date && day1.date >= date_begin)){
  109.  
    day1.hover=true;
  110.  
    }else {
  111.  
    day1.hover=false;
  112.  
    }
  113.  
    }
  114.  
    }
  115.  
    }
  116.  
    }
  117.  
    const Mouseup = () => {
  118.  
    date_begin = ''
  119.  
    for (let i = 0; i < monthlist.value.length; i ) {
  120.  
    for (let j = 0; j < monthlist.value[i].days.length; j ) {
  121.  
    let day1 = monthlist.value[i].days[j];
  122.  
    if(day1.selected && day1.hover){
  123.  
    day1.selected=false;
  124.  
    day1.hover=false;
  125.  
    }else if(!day1.selected &&day1.hover){
  126.  
    day1.selected=true;
  127.  
    day1.hover=false;
  128.  
    }
  129.  
    }
  130.  
    }
  131.  
    }
  132.  
     
  133.  
    const ClickWeek = (month:Month,week:number) => {
  134.  
    for (let j = 0; j < month.days.length; j ) {
  135.  
    if(month.days[j].week===week){
  136.  
    month.days[j].selected=!month.days[j].selected;
  137.  
    }
  138.  
    }
  139.  
    }
  140.  
     
  141.  
     
  142.  
    const ClickReset = () => {
  143.  
    for (let i = 0; i < monthlist.value.length; i ) {
  144.  
    for (let j = 0; j < monthlist.value[i].days.length; j ) {
  145.  
    let day1 = monthlist.value[i].days[j];
  146.  
    day1.selected=false;
  147.  
    day1.hover=false;
  148.  
    }
  149.  
    }
  150.  
    }
  151.  
     
  152.  
    </script>
  153.  
     
  154.  
    <style scoped lang="scss">
  155.  
     
  156.  
    ul {
  157.  
    display: block;
  158.  
    list-style-type: disc;
  159.  
    margin-block-start: 1em;
  160.  
    margin-block-end: 1em;
  161.  
    margin-inline-start: 0px;
  162.  
    margin-inline-end: 0px;
  163.  
    padding-inline-start: 40px;
  164.  
    }
  165.  
     
  166.  
    li {
  167.  
    display: list-item;
  168.  
    text-align: -webkit-match-parent;
  169.  
    }
  170.  
     
  171.  
    ol, ul {
  172.  
    list-style: none;
  173.  
    }
  174.  
     
  175.  
    body, div, li, p, ul {
  176.  
    margin: 0;
  177.  
    padding: 0;
  178.  
    }
  179.  
    div {
  180.  
    display: block;
  181.  
    }
  182.  
     
  183.  
     
  184.  
    .flex-wrap{
  185.  
    flex-wrap: wrap;
  186.  
    display: flex;
  187.  
    }
  188.  
     
  189.  
    .mt-24 {
  190.  
    margin-top: 24px;
  191.  
    }
  192.  
     
  193.  
    .ml-16 {
  194.  
    margin-left: 16px;
  195.  
    }
  196.  
     
  197.  
    .text-primary {
  198.  
    color: #333;
  199.  
    }
  200.  
     
  201.  
    .text-primary, .text-sub, .text-tip {
  202.  
    line-height: 1.5;
  203.  
    }
  204.  
     
  205.  
    .text-center {
  206.  
    text-align: center;
  207.  
    }
  208.  
     
  209.  
    .text-14 {
  210.  
    font-size: 14px;
  211.  
    line-height: 1.5;
  212.  
    }
  213.  
     
  214.  
    .pb-8 {
  215.  
    padding-bottom: 8px;
  216.  
    }
  217.  
     
  218.  
     
  219.  
    .text-sub {
  220.  
    color: #666;
  221.  
    }
  222.  
     
  223.  
    .text-tip {
  224.  
    background-color: #f5f7fa;
  225.  
    color: #999;
  226.  
    }
  227.  
     
  228.  
    .color-red {
  229.  
    color: #f5222d;
  230.  
    }
  231.  
    .relative {
  232.  
    position: relative;
  233.  
    }
  234.  
    .absolute {
  235.  
    position: absolute;
  236.  
    }
  237.  
     
  238.  
     
  239.  
    .calendar-month{
  240.  
    border: 1px solid #d0d0d0;
  241.  
    }
  242.  
     
  243.  
    .calendar-month ul {
  244.  
    width: 252px;
  245.  
    padding: 1px;
  246.  
    }
  247.  
     
  248.  
    .calendar-month li {
  249.  
    text-align: center;
  250.  
    width: 36px;
  251.  
    height: 36px;
  252.  
    line-height: 36px;
  253.  
    font-size: 14px;
  254.  
    cursor: pointer;
  255.  
    -webkit-user-select: none;
  256.  
    -moz-user-select: none;
  257.  
    -ms-user-select: none;
  258.  
    user-select: none;
  259.  
    //border: 1px dashed #8c939d;
  260.  
    }
  261.  
    .calendar-month li span {
  262.  
    font-size: 8px;
  263.  
    bottom: -12px;
  264.  
    left: -2px;
  265.  
    text-align: center;
  266.  
    transform: scale(.6);
  267.  
    }
  268.  
    .calendar-month .isSelected {
  269.  
    background: #b3c8ff;
  270.  
    color: #162d53;
  271.  
    }
  272.  
    .calendar-month .isHover {
  273.  
    background: #6f90e0 !important;
  274.  
    color: #fff!important;
  275.  
    }
  276.  
    .calendar-month .isOver {
  277.  
    background-color: #f0f0f0;
  278.  
    color: #999;
  279.  
    }
  280.  
    //.calendar-month li:nth-of-type(7n), .calendar-month li:nth-of-type(7n - 1) {
  281.  
    // background: #f5f7fa;
  282.  
    //}
  283.  
    //.calendar-month li:hover{
  284.  
    // background-color: #f0f0f0;
  285.  
    // color: #999;
  286.  
    //}
  287.  
    </style>
  288.  
     
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgggijk
系列文章
更多 icon
同类精品
更多 icon
继续加载