使用Vue实现简单的日历,供大家参考,具体内容如下

原理分析:

1.获取当前时间
2.显示当前时间
3.点击增加和减少月份
4.大月和小月的天数

效果演示

初始样式(显示现在的日期时间)

增加一个月

在程序开始之前一定注意:

引入Vue.js架包

代码演示

Body内容

<script type="text/x-template" id="calendar">
   <!-- 年份-->
       <div id="year">
               <!--月份 -->
                    <div class="month">
                        <ul>
                            <li class="arrow" @click="pickPre(currentYear,currentMonth)">❮</li>
                            <li class="year-month" @click="pickYear(currentYear,currentMonth)">
                                <span class="choosen-year" style="color:blue">{{ currentYear }}</span>
                                <span class="choosen-month" style="color:blue">{{ currentMonth }}月</span>
                            </li>
                            <li class="arrow" @click="pickNext(currentYear,currentMonth)">❯</li>
                        </ul>
                    </div>
                    <!-- 星期 -->
                    <ul class="weekdays">
                        <li>一</li>
                        <li>二</li>
                        <li>三</li>
                        <li>四</li>
                        <li>五</li>
                        <li style="color:red">六</li>
                        <li style="color:red">日</li>
                    </ul>
                    <!-- 日期 -->
                    <ul class="days">
                        <!-- 循环-->
                        <li v-for="dayobject in days">
                            <!--本月-->
        
                            <span v-if="dayobject.day.getMonth() 1 != currentMonth" class="other-month">{{ dayobject.day.getDate() }}</span>
        
                            <!--判断天数是否正确-->
                            <span v-else>
                                <!--今天-->
                                <span v-if="dayobject.day.getFullYear() == new Date().getFullYear() && dayobject.day.getMonth() == new Date().getMonth() && dayobject.day.getDate() == new Date().getDate()"
                                    class="active">{{ dayobject.day.getDate() }}</span>
                                <span v-else>{{ dayobject.day.getDate() }}</span>
                            </span>
        
                        </li>
                    </ul>
                </div>
            </script>
<div id="app">
      <calendar></calendar>
</div>

CSS样式

* {
            margin: 0;
            padding: 0;
        }
        
        
        /*日历*/
        
        #calendar {
            width: 98%;
            border: 2px solid #A4A7B0;
            height: 335px;
            margin-left: 0.5%;
        }
        
        .month {
            width: 92%;
            height: 48px;
            border: 2px solid #FFFFFF;
            margin-left: 3%;
            margin-top: 20px;
        }
        
        .month ul {
            margin: 0;
            padding: 0;
            display: flex;
            margin-top: 11px;
            justify-content: space-between;
        }
        
        .year-month {
            flex-direction: column;
            align-items: center;
            justify-content: space-around;
        }
        
        .choosen-year {
            padding: 0 20px;
            font-size: 16px;
            font-weight: 200;
        }
        
        .choosen-month {
            text-align: center;
            font-size: 16px;
            font-weight: 200;
        }
        
        .arrow {
            width: 3%;
            height: 25px;
        }
        
        .arrow1 {
            background: url(left.png) no-repeat 0 0 /100% 100%;
            margin-left: 44px;
        }
        
        .arrow2 {
            background: url(right.png) no-repeat 0 0 /100% 100%;
            margin-right: 44px;
        }
        
        .month ul li {
            color: #999;
            font-size: 20px;
            text-transform: uppercase;
            letter-spacing: 3px;
            list-style: none;
        }
        
        .weekdays {
            margin: 0;
            color: #FFFFFF;
            background: #A4A7B0;
            width: 96.6%;
            margin-top: 26px;
            height: 34px;
            line-height: 34px;
            margin-left: 2.2%;
        }
        
        .weekdays li {
            display: inline-block;
            text-align: center;
            color: #11616f;
            font-size: 14px;
            font-weight: 100;
            width: 12.7%;
        }
        
        .days {
            padding: 0;
            margin: 0;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
        }
        
        .days li {
            list-style-type: none;
            display: inline-block;
            width: 14.2%;
            text-align: center;
            padding-bottom: 3px;
            padding-top: 7px;
            font-size: 12.78px;
            color: rgb(14, 220, 235);
            font-weight: 200;
        }
        
        .days li span span {
            height: 29.5px;
            width: 27px;
            line-height: 29.5px;
            display: inline-block;
        }
        
        .days li .class-30 {
            background: url(bg_30.png) no-repeat 0 0 /100% 100%;
        }
        
        .days li .class-60 {
            background: url(bg_60.png) no-repeat 0 0 /100% 100%;
        }
        
        .days li .class-3060 {
            background: url(bg_3060.png) no-repeat 0 0 /100% 100%;
        }
        
        .days li .other-month {
            padding: 5px;
            color: #84a8ae;
        }

Vue.js内容

Vue.component("calendar", {
            template: "#calendar",
            data: function() {
                return {
                    currentDay: 1,
                    currentMonth: 1,
                    currentYear: 1970,
                    currentWeek: 1,
                    days: [],
                }
            },
            created() {
                let that = this;
                that.initData(null);
            },
            methods: {
                initData: function(cur) {
                    let that = this;
                    let leftcount = 0;
                    let date;
                    if (cur) {
                        date = new Date(cur);
                    } else {
                        let now = new Date();
                        let d = new Date(that.formatDate(now.getFullYear(), now.getMonth(), 1));
                        d.setDate(35);
                        date = new Date(that.formatDate(d.getFullYear(), d.getMonth()   1, 1));
                    }
                    that.currentDay = date.getDate();
                    that.currentYear = date.getFullYear();
                    that.currentMonth = date.getMonth()   1;
                    that.currentWeek = date.getDay(); // 1...6,0
                    if (that.currentWeek == 0) {
                        that.currentWeek = 7;
                    }
                    let str = that.formatDate(that.currentYear, that.currentMonth, that.currentDay);
                    that.days.length = 0;
                    //初始化
                    for (let i = that.currentWeek - 1; i >= 0; i--) {
                        let d = new Date(str);
                        d.setDate(d.getDate() - i);
                        let dayobject = {}; 
                        dayobject.day = d;
                        that.days.push(dayobject); 
                    }
                    for (let i = 1; i <= 35 - that.currentWeek; i  ) {
                        let d = new Date(str);
                        d.setDate(d.getDate()   i);
                        let dayobject = {};
                        dayobject.day = d;
                        that.days.push(dayobject);
                    }

                },
                pickPre: function(year, month) {
                    let that = this;
                    let d = new Date(that.formatDate(year, month, 1));
                    d.setDate(0);
                    that.initData(that.formatDate(d.getFullYear(), d.getMonth()   1, 1));
                },
                pickNext: function(year, month) {
                    let that = this;
                    let d = new Date(that.formatDate(year, month, 1));
                    d.setDate(35);
                    that.initData(that.formatDate(d.getFullYear(), d.getMonth()   1, 1));
                },
                pickYear: function(year, month) {
                    alert(year   ","   month);
                },
                // 返回 类似 2016-01-02 格式的字符串
                formatDate: function(year, month, day) {
                    let y = year;
                    let m = month;
                    if (m < 10) m = "0"   m;
                    let d = day;
                    if (d < 10) d = "0"   d;
                    return y   "-"   m   "-"   d
                },
            }
        })
        let vm = new Vue({
            el: '#app',
        })

到此程序结束。

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

使用Vue实现简单日历效果的更多相关文章

  1. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  2. 无法以编程方式从Android日历中读取重复发生的事件

    p=151&cpage=2#comment-52767来访问内部的android日历数据库.它适用于除重复活动之外的所有条目.光标根本不会返回任何重复发生的事件.有人可以帮助我吗?p=151例:编辑:谷歌似乎开始记录日历提供商.浏览到CalendarProvidier|GoogleDevelopers以获取更多信息.

  3. vue自定义加载指令v-loading占位图指令v-showimg

    这篇文章主要为大家介绍了vue自定义加载指令和v-loading占位图指令v-showimg的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. vue使用动画实现滚动表格效果

    这篇文章主要为大家详细介绍了vue使用动画实现滚动表格效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. 关于Vue 监控数组的问题

    这篇文章主要介绍了Vue 监控数组的示例,主要包括Vue 是如何追踪数据发生变化,Vue 如何更新数组以及为什么有些数组的数据变更不能被 Vue 监测到,对vue监控数组知识是面试比较常见的问题,感兴趣的朋友一起看看吧

  6. Vue子组件props从父组件接收数据并存入data

    这篇文章主要介绍了Vue子组件props从父组件接收数据并存入data的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  7. Vue h函数的使用详解

    本文主要介绍了Vue h函数的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  8. VUE响应式原理的实现详解

    这篇文章主要为大家详细介绍了VUE响应式原理的实现,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

  9. vue+Element ui实现照片墙效果

    这篇文章主要为大家详细介绍了vue+Element ui实现照片墙效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. vue+elemet实现表格手动合并行列

    这篇文章主要为大家详细介绍了vue+elemet实现表格手动合并行列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部