vue实现打卡功能

 更新时间:2022年08月29日 16:28:55   作者:心有阳光热爱生活  
这篇文章主要为大家详细介绍了vue实现打卡功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue实现打卡功能的具体代码,供大家参考,具体内容如下

记录使用vue实现移动端日历打卡样式

template
compareToNow:与当前时间比较
-1:小于当前时间
0:今天
1:大于当前时间

<!-- @click="todo" 实现打卡功能 -->
<div v-if="compareToNow(item) === 1" @click="todo">{{ item.date }}</div>
<!-- @click="todo" 实现补卡功能 -->
 <div v-if="compareToNow(item) === -1" @click="todo" class="otherDay">
<template>
  <div>
    <div class="top-title">
      <div><span @click="lastMonth" class="link">⋘</span></div>
      <div><span>{{year}}年{{month}}月</span></div>
      <div><span @click="nextMonth" class="link">⋙</span></div>
    </div>
    <div class="container" style="border-bottom: 1px solid #cccccc">
      <div v-for="(item,index) in weeks" :key="index">{{ item }}</div>
    </div>
    <div class="container" style="padding: 1vh 1vh 3vh 1vh;">
      <div v-for="(item,index) in data" :key="index">
        <div v-if="compareToNow(item) === 0" style="color: #2d8cf0">{{ item.date }}</div>
        <div v-if="compareToNow(item) === 1">{{ item.date }}</div>
        <div v-if="compareToNow(item) === -1" class="otherDay">
          <div>{{ item.date }}</div>
          <div class="date-desc">补卡</div>
        </div>
      </div>
    </div>
  </div>
</template>

script

<script>
export default {
  data() {
    return {
      today:new Date(),
      now:new Date(),
      weeks:["日","一","二","三","四","五","六"],
      year:"",
      month:"",
      date:"",
      firstDay:"",
      data:[],
    };
  },
  mounted() {
    this.getNow();
  },
  methods:{
    getNow(){
      this.year = this.now.getFullYear();
      this.month = this.now.getMonth() + 1;
      this.date = this.now.getDate();
      this.now.setDate(1);
      this.firstDay = this.now.getDay();
      this.initData();
    },
    getMonthDay(month){
      if ([1,3,5,7,8,10,12].includes(month)) {
        return 31
      } else if ([4,6,9,11].includes(month)) {
        return 30
      } else if (month === 2) {
        //  判断当年是否为闰年
        if (
          (this.year % 4 === 0 && this.year % 100 !== 0) ||
          this.year % 400 === 0
        ) {
          return 29
        } else {
          return 28
        }
      }
    },
    initData(){
      this.data = [];
      let days = this.getMonthDay(this.month);
      for (let i = 0; i < this.firstDay; i++) {
        this.data.push({
          year:"",
          month:"",
          date:"",
        });
      }
      for (let i = 0; i < days; i++) {
        this.data.push(
          {
            year: this.year,
            month: this.month,
            date: i + 1,
          }
        );
      }
    },
    lastMonth(){
      this.now.setMonth(this.now.getMonth() - 1);
      this.getNow();
    },
    nextMonth(){
      this.now.setMonth(this.now.getMonth() + 1);
      this.getNow();
    },
    compareToNow(item){
      if (item.year && item.month && item.date) {
        let date1 = new Date();
        date1.setFullYear(item.year)
        date1.setMonth(item.month - 1)
        date1.setDate(item.date)
        date1.setHours(0)
        date1.setMinutes(0)
        date1.setSeconds(0)
        let now = new Date();
        now.setHours(0)
        now.setMinutes(0)
        now.setSeconds(0)

        if (date1.getTime() > now.getTime()){
          return 1
        }else if (date1.getTime() === now.getTime()){
          return 0
        }else if (date1.getTime() < now.getTime()){
          return -1
        }
      }
    }
  }
}
</script>

style

/* 背景图片 background:url  和 background-size 可注释 */
<style scoped lang="less">
  .top-title{
    display: grid;
    grid-template-columns:repeat(3,1fr);
    grid-auto-rows:40px;
    grid-gap:1rem;
    background-color: #FFFFFF;
    border-bottom: 1px solid #cccccc;
    line-height: 40px;
  }
  .container{
    display: grid;
    grid-template-columns:repeat(7,1fr);
    grid-auto-rows:40px;
    grid-gap:1rem;
    background-color: #FFFFFF;
    line-height: 40px;

    div{
      text-align: center;
    }
  }
  .today{
    background: url("../assets/circle_success.png") no-repeat center center;
    background-size: 95% 95%;
    position: relative;
  }
  .otherDay{
    background: url("../assets/circle_error.png") no-repeat center center;
    background-size: 95% 95%;
    position: relative;
  }
  .link{
    font-size: 14px;
    color: #2d8cf0;
  }
  .date-desc{
    display: block;
    position: absolute;
    top: 6.8vw;
    left: 1.5vw;
    font-size: 2.3vw;
    color: green;
  }
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Vue Electron实现输入法自动刷字数功能详解

    Vue Electron实现输入法自动刷字数功能详解

    这篇文章主要介绍了Vue Electron实现输入法自动刷字数功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • vue列表数据发生变化指令没有更新问题及解决方法

    vue列表数据发生变化指令没有更新问题及解决方法

    这篇文章主要介绍了vue中使用指令,列表数据发生变化指令没有更新问题,本文给出了解决办法,需要的朋友可以参考下
    2020-01-01
  • Vue数据监听方法watch的使用

    Vue数据监听方法watch的使用

    这篇文章主要介绍了Vue数据监听方法watch的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • vue-photo-preview图片预览失效的问题及解决

    vue-photo-preview图片预览失效的问题及解决

    这篇文章主要介绍了vue-photo-preview图片预览失效的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • 如何给element添加一个抽屉组件的方法步骤

    如何给element添加一个抽屉组件的方法步骤

    这篇文章主要介绍了如何给element添加一个抽屉组件的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • Vue的样式绑定详解

    Vue的样式绑定详解

    这篇文章主要为大家详细介绍了Vue的样式绑定,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • vue中的$emit 与$on父子组件与兄弟组件的之间通信方式

    vue中的$emit 与$on父子组件与兄弟组件的之间通信方式

    本文主要对vue 用$emit 与 $on 来进行组件之间的数据传输。重点给大家介绍vue中的$emit 与$on父子组件与兄弟组件的之间通信方式,感兴趣的朋友一起看看
    2018-05-05
  • vue3通过组合键实现换行操作的示例详解

    vue3通过组合键实现换行操作的示例详解

    这篇文章主要为大家详细介绍了vue3如何通过组合键,例如command+Enter、shift+Enter、alt + Enter,实现换行操作,感兴趣的可以了解下
    2024-03-03
  • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验

    浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验

    这次我想给大家介绍的内存泄漏的定位方法,并非工具的使用。而是一些经验的总结,也就是我所知道的 VueJS SSR 中最容易出现内存泄漏的地方,非常具有实用价值,需要的朋友可以参考下
    2018-12-12
  • element-ui tooltip修改背景颜色和箭头颜色的实现

    element-ui tooltip修改背景颜色和箭头颜色的实现

    这篇文章主要介绍了element-ui tooltip修改背景颜色和箭头颜色的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12

最新评论