使用Vue实现简单日历效果

使用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',
        })

到此程序结束。

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

(0)

相关推荐

  • vue实现日历备忘录功能

    用vue写了个日历备忘录的功能,省略了备忘录的增删改查功能. 直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>备忘录</title> <style type="text/css"> #box{ width: 469px; } /*日历*/ *{ padding:

  • Vue编写可显示周和月模式的日历 Vue自定义日历内容的显示

    之前写了一篇周和月日历,但感觉体验不是太好,所以有重新做了一遍,加上了动画.还可以自定义显示日历里的内容. 现在贴出项目源码,我现在是放在CSDN里的下载资源,这里哦 现在我上传带了GitHub上了,可以去这里下载哦,如果觉得好的话希望能给个star,谢谢支持 1.总共分为两个组件(父组件calendar.vue) <template> <div class="calendar-box"> <ul class="calendar-head&quo

  • Vue.js创建Calendar日历效果

    使用 Vue.js 进行数据与视图的绑定,数据更新会让视图自动进行更新,类似 Android 里面的 DataBinding. 实现一个HTML的日历效果. html 部分 <div id="calendar"> <!-- 年份 月份 --> <div class="month"> <ul> <li class="arrow" @click="pickPre(currentYear,

  • Vue实现日历小插件

    本文实例为大家分享了Vue实现日历小插件的具体代码,供大家参考,具体内容如下 先看下效果图吧, 如下 源码可见于我的github 实现关键点: 1.组件的复用以及父子组件传值 很明显每年每个月的月历样式(数据不一样)是一致的,那么自然而然思路就是把每个月作为一个公用组件进行复用十二次,这样就避免了多次重复的代码.每个组件不一样的地方在于年份和月份,而这两个数据我们可以由父组件向子组件进行传值来告诉子组件.关键代码如下: <template> <div class="wrap&q

  • vue实现一个炫酷的日历组件

    公司业务新开了一个商家管理微信H5移动端项目,日历控件是商家管理员查看通过日程来筛选获取某日用户的订单等数据. 如图: 假设今天为2018-09-02 90天前: 90天后; 产品需求: 展示当前日期(服务器时间)前后90天,一共181天的日期. 日历可以左右滑动切换月份. 当月份的如果不在181天区间的,需要置灰并且不可点击. 点击日历绑定的节点的外部,关闭弹窗. 涉及内容: 获取服务器时间,渲染日历数据 vue-touch监听手势滑动事件 ios日期兼容处理 clickOutSide自定义指

  • vue实现简单的日历效果

    最近在项目中遇到了一个需求,在vue中创建一个组件,这个组件显示的是当前的日期,以及在当前的日需要处理的事项,处理的事项的信息会以后端的接口的形式返回. 需求确认后,搭建了一下,在这里记录了一下,现在是简单的实现了这个需求,但是肯定的是后期需要进行修改. vue就不多说了,在vue中使用的是原生JS 效果图(基本没有样式,很low) 现在实现的都是最初级的版本,代码里面的容错,还有一些性能上的处理,并没有书写. 不多说,上代码: 首先是vue的html结构,很简单,里面添加了一些其他时间形式的显

  • 基于Vue2-Calendar改进的日历组件(含中文使用说明)

    一,前言 我是刚学Vue的菜鸟,在使用过程中需要用到日历控件,由于项目中原来是用jQuery写的,因此用了bootstarp的日历控件,但是配合Vue实在有点蛋疼,不够优雅-- 于是网上搜了好久找到了Vue2-Calendar,不用说,挺好用的,但是同时也发现这个组件有些问题,有些功能挺不符合我们的要求,于是着手改了一版 二,改进的功能 在Vue2-Calendar v2.2.4 版基础上作了优化. 1.改进原控件无法切换语言的BUG,支持 lang='zh-CN'和'en'. 2.日历面板增加

  • vue+elementUI实现简单日历功能

    vue+elementUI简单的实现日历功能,供大家参考,具体内容如下 <div class="calender2"> <div class="date-headers"> <div class="date-header"> <div><el-button type="primary" @click="handlePrev"><i class

  • 基于Vue实现支持按周切换的日历

    基于Vue的日历小功能,可根据实际开发情况按每年.每月.每周.进行切换,具体内容如下 <template> <div class="date"> <!-- 年份 月份 --> <div class="month"> <p>{{ currentYear }}年{{ currentMonth }}月</p> </div> <!-- 星期 --> <ul class=&q

  • VUE实现日历组件功能

    哈哈, 就在昨天笔者刚刚在Github 上发布了一个基于VUE的日历组件.过去做日历都是需要引用 jquery moment 引用 fullCalendar.js 的.几者加起来体积庞大不说,也并不是很好使用在vue这种数据驱动的项目里.所以笔者经过一周的拍脑袋,做了一个十分简陋的版本. 简介 目前只支持月视图,该组件是 .vue 文件的形式.所以,大家在使用的时候 是需要node的咯~~~ 安装 npm install vue-fullcalendar DEMO 针对这个组件, 本人做了一个十

随机推荐