基於Vue實現支持按周切換的日歷


實現的效果如下

組件

 1 <template>
 2  <div class="date">
 3    <!-- 年份 月份 -->
 4    <div class="month">
 5     <p>{{ currentYear }}年{{ currentMonth }}月</p>
 6    </div>
 7    <!-- 星期 -->
 8    <ul class="weekdays">
 9     <li>一</li>
10     <li>二</li>
11     <li>三</li>
12     <li>四</li>
13     <li>五</li>
14     <li>六</li>
15     <li>日</li>
16    </ul>
17    <!-- 日期 -->
18    <ul class="days">
19     <li @click="pick(day)" v-for="(day, index) in days" :key="index">
20      <!--本月-->
21      <span v-if="day.getMonth()+1 != currentMonth" class="other-month">{{ day.getDate() }}</span>
22      <span v-else>
23      <!--今天-->
24      <span v-if="day.getFullYear() == new Date().getFullYear() && day.getMonth() == new Date().getMonth() && day.getDate() == new Date().getDate()" class="active">{{ day.getDate() }}</span>
25      <span v-else>{{ day.getDate() }}</span>
26      </span>
27     </li>
28    </ul>
29  </div>
30 </template>

  js部分:目前默認顯示一周,可根據實際情況更改

  1 <script>
  2  
  3  
  4  export default {
  5   name: 'date',
  6  
  7   data () {
  8    return {
  9     currentYear: 1970,  // 年份
 10     currentMonth: 1, // 月份
 11     currentDay: 1,  // 日期
 12     currentWeek: 1,  // 星期
 13     days: [],
 14    }
 15   },
 16  
 17   mounted () {
 18  
 19   },
 20  
 21   created () {
 22    this.initData(null)
 23   },
 24  
 25   methods: {
 26    formatDate (year, month, day) {
 27     const y = year
 28     let m = month
 29     if (m < 10) m = `0${m}`
 30     let d = day
 31     if (d < 10) d = `0${d}`
 32     return `${y}-${m}-${d}`
 33    },
 34  
 35    initData (cur) {
 36     let date = ''
 37     if (cur) {
 38      date = new Date(cur)
 39     } else {
 40      date = new Date()
 41     }
 42     this.currentDay = date.getDate()     // 今日日期 幾號
 43     this.currentYear = date.getFullYear()    // 當前年份
 44     this.currentMonth = date.getMonth() + 1  // 當前月份
 45     this.currentWeek = date.getDay() // 1...6,0  // 星期幾
 46     if (this.currentWeek === 0) {
 47      this.currentWeek = 7
 48     }
 49     const str = this.formatDate(this.currentYear, this.currentMonth, this.currentDay)// 今日日期 年-月-日
 50     this.days.length = 0
 51     // 今天是周日,放在第一行第7個位置,前面6個 這里默認顯示一周,如果需要顯示一個月,則第二個循環為 i<= 35- this.currentWeek
 52     /* eslint-disabled */
 53     for (let i = this.currentWeek - 1; i >= 0; i -= 1) {
 54      const d = new Date(str)
 55      d.setDate(d.getDate() - i)
 56       // console.log(y:" + d.getDate())
 57      this.days.push(d)
 58     }
 59     for (let i = 1; i <= 7 - this.currentWeek; i += 1) {
 60      const d = new Date(str)
 61      d.setDate(d.getDate() + i)
 62      this.days.push(d)
 63     }
 64    },
 65  
 66    // 上個星期
 67    weekPre () {
 68     const d = this.days[0]  // 如果當期日期是7號或者小於7號
 69     d.setDate(d.getDate() - 7)
 70     this.initData(d)
 71    },
 72  
 73    // 下個星期
 74    weekNext () {
 75     const d = this.days[6]  // 如果當期日期是7號或者小於7號
 76     d.setDate(d.getDate() + 7)
 77     this.initData(d)
 78    },
 79  
 80    // 上一個月  傳入當前年份和月份
 81    pickPre (year, month) {
 82     const d = new Date(this.formatDate(year, month, 1))
 83     d.setDate(0)
 84     this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1))
 85    },
 86  
 87  
 88    // 下一個月  傳入當前年份和月份
 89    pickNext (year, month) {
 90     const d = new Date(this.formatDate(year, month, 1))
 91     d.setDate(35)
 92     this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1))
 93    },
 94  
 95    // 當前選擇日期
 96    pick (date) {
 97     alert(this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate()))
 98    },
 99   },
100  }
101 </script>

 

 1 <style lang="scss">
 2  @import "~base";
 3  
 4  .date {
 5   height: px2rem(180);
 6   color: #333;
 7  
 8   .month {
 9    font-size: px2rem(24);
10    text-align: center;
11    margin-top: px2rem(20);
12   }
13  
14   .weekdays {
15    display: flex;
16    font-size: px2rem(28);
17    margin-top: px2rem(20);
18  
19    li {
20     flex: 1;
21     text-align: center;
22    }
23   }
24  
25   .days {
26    display: flex;
27  
28    li {
29     flex: 1;
30     font-size: px2rem(30);
31     text-align: center;
32     margin-top: px2rem(10);
33     line-height: px2rem(60);
34  
35     .active {
36      display: inline-block;
37      width: px2rem(60);
38      height: px2rem(60);
39      color: #fff;
40      border-radius: 50%;
41      background-color: #fa6854;
42     }
43  
44     .other-month {
45      color: #e4393c;
46     }
47    }
48   }
49  }
50 </style>

 

 裝載自:基於Vue實現支持按周切換的日歷

原文:https://www.jb51.net/article/120613.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM