最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Vue3實(shí)現(xiàn)日歷組件的示例代碼

 更新時(shí)間:2023年04月23日 10:11:17   作者:飛仔FeiZai  
日歷在很多地方都可以使用的到,這篇文章主要介紹了如何利用vue3實(shí)現(xiàn)簡單的日歷控件,文中通過示例代碼講解詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

以下是一個(gè)基于 Vue 3 實(shí)現(xiàn)的簡單日歷組件的代碼示例。這個(gè)日歷組件包含了前一個(gè)月、當(dāng)前月、下一個(gè)月的日期,并且可以支持選擇日期、切換月份等功能。

<template>
  <div class="calendar">
    <div class="header">
      <button class="prev" @click="prevMonth">&lt;</button>
      <div class="title">{{ title }}</div>
      <button class="next" @click="nextMonth">&gt;</button>
    </div>
    <div class="weekdays">
      <div v-for="day in daysOfWeek" :key="day" class="day">{{ day }}</div>
    </div>
    <div class="days">
      <div
        v-for="day in days"
        :key="day.date"
        :class="{
          today: isToday(day),
          selected: isSelected(day),
          notCurrentMonth: isNotCurrentMonth(day),
        }"
        @click="select(day)"
      >
        {{ day.day }}
      </div>
    </div>
  </div>
</template>

<script>
  import { ref, computed } from "vue";

  export default {
    name: "FeiCalendar",
    props: {
      selectedDate: Date,
    },
    emits: ["update:selectedDate"],
    setup(props, { emit }) {
      const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
      const currentDate = ref(new Date());
      const selectedDate = ref(props.selectedDate || currentDate.value);

      const daysOfWeek = computed(() => {
        return weekdays;
      });

      const days = computed(() => {
        const year = currentDate.value.getFullYear();
        const month = currentDate.value.getMonth();
        const daysInMonth = new Date(year, month + 1, 0).getDate();
        const daysInLastMonth = new Date(year, month, 0).getDate();
        const firstDayOfMonth = new Date(year, month, 1).getDay();

        const days = [];
        let day = 1;
        let lastMonthDay = daysInLastMonth - firstDayOfMonth + 1;
        let nextMonthDay = 1;

        for (let i = 0; i < 6 * 7; i++) {
          if (i < firstDayOfMonth) {
            days.push({
              date: new Date(year, month - 1, lastMonthDay),
              day: lastMonthDay,
              isLastMonth: true,
              isNextMonth: false,
            });
            lastMonthDay++;
          } else if (i >= firstDayOfMonth + daysInMonth) {
            days.push({
              date: new Date(year, month + 1, nextMonthDay),
              day: nextMonthDay,
              isLastMonth: false,
              isNextMonth: true,
            });
            nextMonthDay++;
          } else {
            const date = new Date(year, month, day);
            days.push({ date, day, isLastMonth: false, isNextMonth: false });
            day++;
          }
        }

        return days;
      });

      const title = computed(
        () =>
          `${currentDate.value.toLocaleString("default", {
            month: "long",
          })} ${currentDate.value.getFullYear()}`
      );

      const prevMonth = () => {
        currentDate.value = new Date(
          currentDate.value.getFullYear(),
          currentDate.value.getMonth() - 1,
          1
        );
      };

      const nextMonth = () => {
        currentDate.value = new Date(
          currentDate.value.getFullYear(),
          currentDate.value.getMonth() + 1,
          1
        );
      };

      const isToday = (day) => {
        const today = new Date();
        return day.date.toDateString() === today.toDateString();
      };

      const isSelected = (day) => {
        return day.date.toDateString() === selectedDate.value.toDateString();
      };

      const isNotCurrentMonth = (day) => {
        return day.isLastMonth || day.isNextMonth;
      };

      const select = (day) => {
        selectedDate.value = day.date;
        emit("update:selectedDate", day.date);
      };

      return {
        daysOfWeek,
        days,
        title,
        prevMonth,
        nextMonth,
        isToday,
        isSelected,
        isNotCurrentMonth,
        select,
      };
    },
  };
</script>

<style>
  .calendar {
    max-width: 500px;
    margin: 0 auto;
    font-family: Arial, sans-serif;
  }

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }

  .title {
    font-size: 18px;
    font-weight: bold;
  }

  .weekdays {
    display: flex;
    justify-content: space-around;
    margin-bottom: 10px;
  }

  .day {
    width: 30px;
    height: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 50%;
  }

  .days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    grid-gap: 10px;
  }

  .today {
    background-color: lightblue;
  }

  .selected {
    background-color: blue;
    color: white;
  }

  .notCurrentMonth {
    color: #ccc;
  }
</style>

使用該組件時(shí),可以將selectedDate屬性綁定到一個(gè)父組件中的數(shù)據(jù),這個(gè)數(shù)據(jù)將會存儲選中的日期。例如:

<template>
  <div>
    <!-- 用法一 -->
    <FeiCalendar
      :selectedDate="selectedDate"
      @update:selectedDate="onSelectedDateUpdated"
    />
    <!-- 用法二 -->
    <!-- <FeiCalendar v-model:selectedDate="selectedDate" /> -->
    <p>Selected date: {{ selectedDate }}</p>
  </div>
</template>

<script>
  import FeiCalendar from "./FeiCalendar.vue";

  export default {
    components: {
      FeiCalendar,
    },
    data() {
      return {
        selectedDate: new Date(),
      };
    },
    watch: {
      selectedDate(nv) {
        console.log("nv", nv);
      },
    },
    methods: {
      onSelectedDateUpdated(selectedDate) {
        this.selectedDate = selectedDate;
      },
    },
  };
</script>

這是一個(gè)簡單的示例,可以根據(jù)自己的需求對代碼進(jìn)行修改和擴(kuò)展。

到此這篇關(guān)于基于Vue3實(shí)現(xiàn)日歷組件的示例代碼的文章就介紹到這了,更多相關(guān)Vue3日歷組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Nuxt.js結(jié)合Serverless構(gòu)建無服務(wù)器應(yīng)用

    Nuxt.js結(jié)合Serverless構(gòu)建無服務(wù)器應(yīng)用

    Nuxt.js是一個(gè)基于Vue.js的框架,結(jié)合Serverless架構(gòu),Nuxt.js可以讓你構(gòu)建高度可擴(kuò)展、成本效益高的無服務(wù)器應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • vue.js使用v-if實(shí)現(xiàn)顯示與隱藏功能示例

    vue.js使用v-if實(shí)現(xiàn)顯示與隱藏功能示例

    這篇文章主要介紹了vue.js使用v-if實(shí)現(xiàn)顯示與隱藏功能,結(jié)合簡單實(shí)例形式分析了使用v-if進(jìn)行判斷實(shí)現(xiàn)元素的顯示與隱藏功能,需要的朋友可以參考下
    2018-07-07
  • vue實(shí)現(xiàn)計(jì)步器功能

    vue實(shí)現(xiàn)計(jì)步器功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)計(jì)步器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Vue子組件監(jiān)聽父組件值的變化

    Vue子組件監(jiān)聽父組件值的變化

    這篇文章主要介紹了Vue子組件監(jiān)聽父組件值的變化方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue自定義render統(tǒng)一項(xiàng)目組彈框功能

    Vue自定義render統(tǒng)一項(xiàng)目組彈框功能

    這篇文章主要介紹了Vue自定義render統(tǒng)一項(xiàng)目組彈框功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Vue3中ref與reactive的詳解與擴(kuò)展

    Vue3中ref與reactive的詳解與擴(kuò)展

    在vue3中對響應(yīng)式數(shù)據(jù)的聲明官方給出了ref()和reactive()這兩種方式,下面這篇文章主要給大家介紹了關(guān)于Vue3中ref與reactive的相關(guān)資料,需要的朋友可以參考下
    2021-06-06
  • vue使用once修飾符,使事件只能觸發(fā)一次問題

    vue使用once修飾符,使事件只能觸發(fā)一次問題

    這篇文章主要介紹了vue使用once修飾符,使事件只能觸發(fā)一次問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue組件Prop傳遞數(shù)據(jù)的實(shí)現(xiàn)示例

    vue組件Prop傳遞數(shù)據(jù)的實(shí)現(xiàn)示例

    本篇文章主要介紹了vue組件Prop傳遞數(shù)據(jù)的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Vue組件中常見的props默認(rèn)值陷阱問題

    Vue組件中常見的props默認(rèn)值陷阱問題

    這篇文章主要介紹了避免Vue組件中常見的props默認(rèn)值陷阱,本文通過問題展示及解決方案給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Vite使用報(bào)錯(cuò)解決方法合集

    Vite使用報(bào)錯(cuò)解決方法合集

    這篇文章主要給大家介紹了關(guān)于Vite使用報(bào)錯(cuò)解決方法的相關(guān)資料,這篇文中通過圖文以及代碼將遇到的一些報(bào)錯(cuò)介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vite具有一定的借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08

最新評論

灵川县| 常宁市| 连平县| 龙江县| 临颍县| 安丘市| 林口县| 车致| 宝鸡市| 甘德县| 海南省| 云和县| 安国市| 美姑县| 仙居县| 南昌市| 祁连县| 日喀则市| 上犹县| 永修县| 东港市| 巫山县| 漳浦县| 门源| 天柱县| 福清市| 嵩明县| 尉氏县| 三明市| 马尔康县| 故城县| 原阳县| 资阳市| 磐石市| 安图县| 马尔康县| 望都县| 遂溪县| 弥勒县| 会同县| 漳浦县|