美文网首页
vue实现日历

vue实现日历

作者: 前端伏地魔 | 来源:发表于2020-10-13 00:02 被阅读0次

目前手上的项目中需要使用日历签到,甲方粑粑要求偏绿色系,而且必须完全照着设计来。ui框架中的日历组件拿来改了又改,根本过不了关,技术菜又看不懂源码,没办法只有手写了。顺便记录一下,以后搬砖修修补补继续用。
大概就是这个丑样子 ☟↓

char.png

几步搞定:
1-首先获取当前年份和月份
2-判断是平年还是闰年(闰年一般不是整百除以4整除,或者整百数除以400整除)
3-每点击上一月或者下月都要做对应的月份加减,月份为1或者12时做对应的年份加减,再次判断是平年还是闰年
4-获取每个月的一号是周几,再在日期数组前面添加空字符占位

data(){
      return {
           year:null,//年份
           month:null,//月份
           days:[],//当月的日期列表
           daynum:[31,28,31,30,31,30,31,31,30,31,30,31]//1-12月每个月的天数
     }
},
created(){
     let time=new Date()
     this.year=time.getFullYear()
     this.month=time.getMonth()+1

     this.isyear(this.year)//判断平年闰年
     this.nowmonth()
},
methods:{
     // 判断平年闰年
     isyear(val){
          if(val%4==0 && val%100!=0 || val%100==0 && val%400==0){
               this.daynum[1]=29
          }else{
               this.daynum[1]=28
         }
      },
     // 计算当月多少天
     nowmonth(){
           var daylength=this.daynum[this.month-1]
           this.days.splice(0)  //清空原数组
           for(let i=0;i<daylength;i++){
                 this.days.push(i+1)//根据获取的天数向数组中添加日期
           }

          //获取当月1号是周几,然后向数组中添加空字符串占位
           var times=new Date(this.year,this.month-1,1).getDay()
           if(times==0){//如果times是0则在前面添加6个空字符(视情况,有的星期天排在最前面)
                 times=6
           }else{
                 times-- //返回6则添加5个空字符,以此内推,返回1则不添加
           }
           for(let f=0;f<times;f++){
                 this.days.unshift(" ")
           }
     },
     // 上个月
     lastMonth(){
          if(this.month==1){
                this.month=12
                this.year--
                this.isyear(this.year)
          }else{
                 this.month--
          }
          this.nowmonth()
      },
      // 下个月
      nextMonth(){
          if(this.month==12){
               this.month=1
               this.year++
               this.isyear(this.year)
          }else{
               this.month++
          }
          this.nowmonth()
     }
}

最后再附上一点点html代码

<div id="mycha">
        <div class="thead">
            <img src="./image/last.png" alt="" @click="lastMonth">
            <p>{{year}}年{{month}}月</p>
            <img src="./image/next.png" alt="" @click="nextMonth">
        </div>

        <div class="calendarwa">
            <div class="secd">
                <p>一</p>
                <p>二</p>
                <p>三</p>
                <p>四</p>
                <p>五</p>
                <p>六</p>
                <p>日</p>
            </div>

            <ul>
                <li v-for="item in days" :key="item" :class="{}">{{item}}</li>
            </ul>
        </div>
    </div>

相关文章

  • vue实现日历

    目前手上的项目中需要使用日历签到,甲方粑粑要求偏绿色系,而且必须完全照着设计来。ui框架中的日历组件拿来改了又改,...

  • Vue实现有价格日历

    1.分析 *上面月份是一部分 *下面的日期是一个整体 *根据获取到的数据先获取到有几个月 *再给每个月赋值 *月份...

  • Vue纯JS实现日历

    效果图 不说废话,先上效果图: 实现代码 核心方法是pushDays() 思路 为了日历的完整和美观,采用的6行 ...

  • 打卡日历,日历前端实现,日历特殊标识后端提供(Vue)

    打卡/签到/日历,日历数据,样式前端处理,后端只需要提供打卡日期即可,先看结果是不是你想要的,其实稍加改动可以广泛...

  • vue实现最简单的日历板

    最近遇到的一个项目需要实现日历板的功能,前期技术准备的时候随手写了一个简版日历板,样式几乎没有,功能也只有月份的切...

  • vue-event-calendar

    vue-event-calendar vue-event-calendar是一款简单小巧的事件日历组件,针对Vue...

  • vue总结

    vue路由守卫 vue实现双向绑定原理 Vue生命周期: vue跨域方式 vue-router实现原理 vue-r...

  • vue 日历插件

    本人是一个刚刚入职小码农,用于记录自己学习的过程,写的不好请见谅。 为什么要改写日历插件? 写多了vue项目看到原...

  • vue日历组件

    效果图 组件代码

  • react-hash-calendar,移动端日期时间选择插件

    按照惯例,先上效果图 vue 版本同款日历:https://github.com/TangSY/vue-hash-...

网友评论

      本文标题:vue实现日历

      本文链接:https://www.haomeiwen.com/subject/wnhmpktx.html