欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于vue手動實現一個日歷組件

 更新時間:2024年01月24日 15:21:05   作者:通往自由之路  
這篇文章主要為大家詳細介紹了如何基于vue手動實現一個日歷組件,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以跟隨小編一起學習一下

最近需要一個日歷,當前能用的現成的組件庫也有日歷組件,但因為不滿足需求,所以決定自己搞一個。

最終效果:

實現的功能:上個月、當月、下個月和回到今天切換功能。

對應的html部分

   <div class="plan-zone">
            <div class="btn-group">
              <div class="left-btn">
                <el-button-group>
                  <el-button @click="prevMonth" type="primary"
                    >上一個月</el-button
                  >
                  <el-button type="primary" @click="goToCurrentMonth">{{
                    getCurDate()
                  }}</el-button>
                  <el-button @click="nextMonth" type="primary"
                    >下一個月</el-button
                  >
                </el-button-group>
                <el-button type="primary" @click="goToCurrentDay"
                  >回到今天</el-button
                >
              </div>
              <div class="right-btn">
                <button class="new">新安排</button>
                <button class="ing">進行中</button>
                <button class="finish">已完成</button>
              </div>
            </div>
            <!-- <el-calendar v-model="value"> </el-calendar> -->
            <table class="parent-table">
              <thead>
                <th>周一</th>
                <th>周二</th>
                <th>周三</th>
                <th>周四</th>
                <th>周五</th>
                <th>周六</th>
                <th>周日</th>
              </thead>
              <tbody>
                <tr v-for="(week, windex) in weeks" :key="windex">
                  <td
                    v-for="(day, dindex) in week"
                    :class="{ highlight: isToday(day.date) }"
                    :key="dindex"
                  >
                    <div
                      class="content"
                      :class="{
                        faded: !isCurrentMonth(day.date),
                      }"
                    >
                      <div class="top-day">{{ day.date.getDate() }}日</div>
                      <div class="middle-event"></div>
                      <div class="bottom-event"></div>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>

對應scss部分

.faded {
  opacity: 0.3;
}
.highlight {
  background: rgba(255, 220, 40, 0.15);
}
.plan-zone {
  margin-top: 10px;
  .btn-group {
    display: flex;
    justify-content: space-between;
    .right-btn {
      button.new {
        background-color: #fff;
        border: 1px solid #fff;
        color: #409eef;
        position: relative;
        &::before {
          content: "";
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: absolute;
          top: 7px;
          left: -3px;
          background-color: #409eef;
        }
      }
      button.ing {
        background-color: #fff;
        border: 1px solid #fff;
        color: #ff974a;
        position: relative;
        &::before {
          content: "";
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: absolute;
          top: 7px;
          left: -3px;
          background-color: #ff974a;
        }
      }
      button.finish {
        background-color: #fff;
        border: 1px solid #fff;
        color: #3dd599;
        position: relative;
        &::before {
          content: "";
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: absolute;
          top: 7px;
          left: -3px;
          background-color: #3dd599;
        }
      }
    }
  }
}
.parent-table {
  border-collapse: collapse;
  table-layout: fixed;
  width: 100%;
  margin-top: 20px;
  th,
  td {
    width: 14.4%;
    border: 1px solid #ddd;
  }
  td {
    padding: 2px 3px;

    .content {
      position: relative;
      min-height: 80px;
    }
    vertical-align: top;
    .top-day {
      text-align: right;
      font-size: 13px;
    }
  }
}
.table-date {
  display: flex;
  > div {
    flex: 1;
  }
}

對應的JavaScript部分

export default {
  data() {
    return {
      current: new Date(),
      today: new Date(),
    };
  },
  computed: {
    weeks() {
      return this.getMonthData(
        this.current.getFullYear(),
        this.current.getMonth() + 1
      );
    },
  },
  methods: {
    getCurDate() {
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1; // getMonth() returns a zero-based value (0-11)
      if (month < 10) {
        month = "0" + month; // add a leading zero if the month is a single digit
      }
      return year + "-" + month;
    },
    isToday(date) {
      let today = new Date();
      return (
        date.getDate() === today.getDate() &&
        date.getMonth() === today.getMonth() &&
        date.getFullYear() === today.getFullYear()
      );
    },
    goToCurrentDay() {
      this.current = new Date(this.today);
    },
    isCurrentMonth(date) {
      return date.getMonth() === this.current.getMonth();
    },
    prevMonth() {
      this.current.setMonth(this.current.getMonth() - 1);
      this.current = new Date(this.current);
      // console.log(this.current.getFullYear(), 'dedede')
    },
    nextMonth() {
      this.current.setMonth(this.current.getMonth() + 1);
      this.current = new Date(this.current);
    },
    goToCurrentMonth() {
      this.current = new Date(this.today);
    },
    getMonthData(year, month) {
      let weeks = [];
      let firstDay = new Date(year, month - 1, 1); // 這個月的第一天
      let lastDayOfCurrentMonth = new Date(year, month, 0); // 這個月的最后一天
      let lastDayOfPrevMonth = new Date(year, month - 1, 0); // 上個月的最后一天

      //這里的0有一個特殊的意義,它可以返回上個月的最后一天。也就是說,如果你想知道某個月有多少天,你可以創(chuàng)建一個日期對象,年份和月份設置為你想知道的月份,日期設置為0,然后調用getDate()方法,返回的就是那個月的天數。
      // new Date(year, month - 1, 0) 最后一個參數,超過當月天數重新排序,比如1月31天,最后一個參數為33返回2
      let startDayOfWeek = firstDay.getDay() === 0 ? 7 : firstDay.getDay(); // 開始是周幾
      let dayCount = 1; // 當前日期的變量,初始值為1
      // 上一個月的天數
      let prevMonthDayCount = lastDayOfPrevMonth.getDate() - startDayOfWeek + 2; // 這是為了在日歷中填充上個月的日期。
      // console.log(lastDayOfPrevMonth.getDate(), startDayOfWeek, prevMonthDayCount)
      for (let i = 0; i < 6; i++) {
        let week = [];
        for (let j = 0; j < 7; j++) {
         // 日期為上個月的日期,然后將這個日期對象添加到`week`數組中,同時`prevMonthDayCount`加1。
          if (i === 0 && j < startDayOfWeek - 1) {

            week.push({ date: new Date(year, month - 2, prevMonthDayCount++) });
            // 日期為下個月的日期,然后將這個日期對象添加到`week`數組中,同時`dayCount`加1
          } else if (dayCount > lastDayOfCurrentMonth.getDate()) {
            week.push({
              date: new Date(
                year,
                month,
                dayCount++ - lastDayOfCurrentMonth.getDate()
              ),
            });
           // 日期為這個月的日期,然后將這個日期對象添加到`week`數組中,同時`dayCount`加1
          } else {
            week.push({ date: new Date(year, month - 1, dayCount++) });
          }
        }
        weeks.push(week);
      }
      return weeks;
    }

  },
};

關鍵是上面的getMonthData方法。理解這個方法就理解了這個組件。

以上就是基于vue手動實現一個日歷組件的詳細內容,更多關于vue日歷組件的資料請關注腳本之家其它相關文章!

相關文章

  • Vue使用Cropper實現圖片裁剪功能

    Vue使用Cropper實現圖片裁剪功能

    圖片裁剪功能無論是用戶頭像的裁剪,還是圖片內容的精確調整,都成為了提升用戶體驗的關鍵一環(huán),本文將詳細介紹如何在Vue.js項目中集成并使用Cropper.js實現一個強大的圖片裁剪組件,需要的可以參考下
    2024-11-11
  • Vue3造輪子之Typescript配置highlight過程

    Vue3造輪子之Typescript配置highlight過程

    這篇文章主要介紹了Vue3造輪子之Typescript配置highlight過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue中的模態(tài)對話框組件實現過程

    vue中的模態(tài)對話框組件實現過程

    這篇文章主要介紹了vue中的模態(tài)對話框組件實現過程,通過template定義組件,并添加相應的對話框樣式,需要的朋友可以參考下
    2018-05-05
  • 在vue中:style 的使用方式匯總

    在vue中:style 的使用方式匯總

    在Vue開發(fā)中使用:style綁定樣式是常見需求,應注意:class與:style的配合使用,錯誤的使用可能導致樣式不生效,正確的方法是使用數組綁定多個樣式,這些技巧有助于提高開發(fā)效率和保持代碼整潔,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • Vue可自定義tab組件用法實例

    Vue可自定義tab組件用法實例

    在本篇文章里小編給大家分享了關于Vue可自定義tab組件用法實例以及相關知識點,需要的朋友們參考下。
    2019-10-10
  • Vue3實現自定義Input組件的示例詳解

    Vue3實現自定義Input組件的示例詳解

    這篇文章主要為大家詳細介紹了如何使用Vue3自定義實現一個類似el-input的組件,可以v-model雙向綁定,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • Vue + Element-ui的下拉框el-select獲取額外參數詳解

    Vue + Element-ui的下拉框el-select獲取額外參數詳解

    這篇文章主要介紹了Vue + Element-ui的下拉框el-select獲取額外參數詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 解讀為什么vue前端項目要使用Nodejs

    解讀為什么vue前端項目要使用Nodejs

    這篇文章主要介紹了解讀為什么vue前端項目要使用Nodejs問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • vue中formdata傳值給后臺時參數為空的問題

    vue中formdata傳值給后臺時參數為空的問題

    這篇文章主要介紹了vue中formdata傳值給后臺時參數為空的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • vue.js入門教程之計算屬性

    vue.js入門教程之計算屬性

    Vue.js 的內聯表達式非常方便,但它最合適的使用場景是簡單的布爾操作或字符串拼接。如果涉及更復雜的邏輯,你應該使用計算屬性。這篇文章我們將一起學習vue.js的計算屬性。什么是計算屬性,為什么要用這東西呢?通過下面這篇文章你將解決這些問題,下面來一起看看吧。
    2016-09-09

最新評論