vue之手把手教你写日历组件

---恢复内容开始---

1.日历组件

1.分析功能:日历基本功能,点击事件改变日期,样式的改变

1.结构分析:html

1.分为上下两个部分

2.上面分为左按钮,中间内容展示,右按钮

下面分为周几展示和日期展示

3.基本结构页面html书写

<template>    <div class="calender2">        <div class="date-header">            <div class="pre-month"></div>            <div class="show-date">2019年8月9日</div>            <div class="next-month"></div>        </div>        <div class="date-content">            <div class="week-header">                <div                 v-for="item in [‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘]"                :key= item                >{{ item }}</div>            </div>            <div class="week-day">                <div                 class="every-day"                v-for="item in 42"                :key="item"                >{{ item }}</div>            </div>        </div>    </div></template>
*{    margin: 0px;    border: 0px;    list-style: none;}.calender2{    position: absolute;    top: 50%;    left: 50%;    transform: translate(-50%,-50%);    height:380px;    width:420px;    border: 1px solid #ccc;}.date-header{    margin-left: 10px;    height: 40px;    width: 350px;    line-height: 40px;    text-align: center;}.pre-month{    position: absolute;    display: inline-block;    height: 0px;    width:0px;    border:20px solid ;    border-color: transparent rgb(35, 137, 206) transparent transparent;}.next-month{    position: absolute;    display: inline-block;    height: 0px;    width:0px;    border:20px solid ;    border-color: transparent transparent transparent  rgb(35, 137, 206);}.show-date{    margin-left: 40px;    margin-top: 0px;    display: inline-block;    line-height: 40px;    text-align: center;    width: 310px;    color: rgb(35, 137, 206);}.week-header{    background: rgb(35, 137, 206);    color: #fff;    font-size: 14px;    text-align: center;    line-height: 20px;}.week-header div{    margin: 0;    padding: 0;    display: inline-block;    height: 20px;    width: 60px;}.every-day{    display: inline-block;    height: 50px;    width: 60px;    text-align: center;    line-height: 50px;}.other-day{    color: #ccc;}.now-day{    background: rgb(35, 137, 206);}.active-day{    /* padding: 2px */    /* border-sizing:content-box; */    border: 2px solid rgb(35, 137, 206);}</style>

4.一些事件以及逻辑

1.使得当前的日期为今天的日期

            <div class="show-date">{{ year }}年{{ month }}月{{ day }}日</div>
data(){        return{            year:null,            month:null,            day:null        }    },    created(){        this.getInitDate();    },    methods:{        getInitDate(){            const date = new Date();            this.year = date.getFullYear();            this.month = date.getUTCMonth() + 1;            this.day = date.getDate();        }    }

2.设置该月日期起始值(找到一号是在哪里)

beginDay(){    return new Date(this.year, this.mounth - 1, 1).getDay();}

3.当月天数字体正常显示

<div v-if="item - beginDay >= 0 && item - beginDay <= curDays">{{ item - beginDay }}</div>?

4.当月天数之前的部分变灰,外加正常显示日期

注意几个数学问题:

1.当前月天数日期
2.上月剩余天数
3.此月显示的下月天数
<div      v-if="item - beginDay > 0 && item - beginDay <= curDays"     >{{ item - beginDay }}</div><div      class="other-day"      v-else-if="item - beginDay <= 0"     >{{ item - beginDay + prevDays }}</div><div      class="other-day"      v-else>{{ item - beginDay -curDays }}</div>

5.能知道当前日期,能点击其他日期,并且会有相应的变化

知道当前日期:

 this.curDate = `${this.year}-${this.month}-${this.day}`

判断今天是不是当前日期,并且给一个样式:

‘now-day‘:`${year}-${month}-${item - beginDays}` == curDate

当点击当月有的日期的时候会根据你的点击显示的日期发生变化

判断是点击的那一天:

‘active-day‘:`${year}-${month}-${item - beginDay}` === `${year}-${month}-${day}`

点击这一天,绑定点击事件

@click="handleClickDay(item - beginDay)"
handleClickDay(day){    this.day = day}

6.前后两个按钮的功能

            <div class="pre-month" @click="handlePrev"></div>            <div class="next-month" @click="handleNext"></div>
handlePrev(){            if(this.month == 1){                this.month = 12                this.year--            }else{                this.month--            }        },        handleNext(){            if(this.month == 12){                this.month = 1                this.year++            }else{                this.month++            }        }

7.判断点击的是否为当月的最后一天

computedDay(){            const allDay = new Date(this.year, this.month, 0).getDate();            if(this.day > allDay){                this.day = allDay;            }        }

将这个函数分别在handlePrev(),handleNext()里面执行-------注意是this.computedDay();

完成

---恢复内容结束---

1.日历组件

1.分析功能:日历基本功能,点击事件改变日期,样式的改变

1.结构分析:html

1.分为上下两个部分

2.上面分为左按钮,中间内容展示,右按钮

下面分为周几展示和日期展示

3.基本结构页面html书写

<template>    <div class="calender2">        <div class="date-header">            <div class="pre-month"></div>            <div class="show-date">2019年8月9日</div>            <div class="next-month"></div>        </div>        <div class="date-content">            <div class="week-header">                <div                 v-for="item in [‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘]"                :key= item                >{{ item }}</div>            </div>            <div class="week-day">                <div                 class="every-day"                v-for="item in 42"                :key="item"                >{{ item }}</div>            </div>        </div>    </div></template>
*{    margin: 0px;    border: 0px;    list-style: none;}.calender2{    position: absolute;    top: 50%;    left: 50%;    transform: translate(-50%,-50%);    height:380px;    width:420px;    border: 1px solid #ccc;}.date-header{    margin-left: 10px;    height: 40px;    width: 350px;    line-height: 40px;    text-align: center;}.pre-month{    position: absolute;    display: inline-block;    height: 0px;    width:0px;    border:20px solid ;    border-color: transparent rgb(35, 137, 206) transparent transparent;}.next-month{    position: absolute;    display: inline-block;    height: 0px;    width:0px;    border:20px solid ;    border-color: transparent transparent transparent  rgb(35, 137, 206);}.show-date{    margin-left: 40px;    margin-top: 0px;    display: inline-block;    line-height: 40px;    text-align: center;    width: 310px;    color: rgb(35, 137, 206);}.week-header{    background: rgb(35, 137, 206);    color: #fff;    font-size: 14px;    text-align: center;    line-height: 20px;}.week-header div{    margin: 0;    padding: 0;    display: inline-block;    height: 20px;    width: 60px;}.every-day{    display: inline-block;    height: 50px;    width: 60px;    text-align: center;    line-height: 50px;}.other-day{    color: #ccc;}.now-day{    background: rgb(35, 137, 206);}.active-day{    /* padding: 2px */    /* border-sizing:content-box; */    border: 2px solid rgb(35, 137, 206);}</style>

4.一些事件以及逻辑

1.使得当前的日期为今天的日期

            <div class="show-date">{{ year }}年{{ month }}月{{ day }}日</div>
data(){        return{            year:null,            month:null,            day:null        }    },    created(){        this.getInitDate();    },    methods:{        getInitDate(){            const date = new Date();            this.year = date.getFullYear();            this.month = date.getUTCMonth() + 1;            this.day = date.getDate();        }    }

2.设置该月日期起始值(找到一号是在哪里)

beginDay(){    return new Date(this.year, this.mounth - 1, 1).getDay();}

3.当月天数字体正常显示

<div v-if="item - beginDay >= 0 && item - beginDay <= curDays">{{ item - beginDay }}</div>?

4.当月天数之前的部分变灰,外加正常显示日期

注意几个数学问题:

1.当前月天数日期
2.上月剩余天数
3.此月显示的下月天数
<div      v-if="item - beginDay > 0 && item - beginDay <= curDays"     >{{ item - beginDay }}</div><div      class="other-day"      v-else-if="item - beginDay <= 0"     >{{ item - beginDay + prevDays }}</div><div      class="other-day"      v-else>{{ item - beginDay -curDays }}</div>

5.能知道当前日期,能点击其他日期,并且会有相应的变化

知道当前日期:

 this.curDate = `${this.year}-${this.month}-${this.day}`

判断今天是不是当前日期,并且给一个样式:

‘now-day‘:`${year}-${month}-${item - beginDays}` == curDate

当点击当月有的日期的时候会根据你的点击显示的日期发生变化

判断是点击的那一天:

‘active-day‘:`${year}-${month}-${item - beginDay}` === `${year}-${month}-${day}`

点击这一天,绑定点击事件

@click="handleClickDay(item - beginDay)"
handleClickDay(day){    this.day = day}

6.前后两个按钮的功能

            <div class="pre-month" @click="handlePrev"></div>            <div class="next-month" @click="handleNext"></div>
handlePrev(){            if(this.month == 1){                this.month = 12                this.year--            }else{                this.month--            }        },        handleNext(){            if(this.month == 12){                this.month = 1                this.year++            }else{                this.month++            }        }

7.判断点击的是否为当月的最后一天

computedDay(){            const allDay = new Date(this.year, this.month, 0).getDate();            if(this.day > allDay){                this.day = allDay;            }        }

将这个函数分别在handlePrev(),handleNext()里面执行-------注意是this.computedDay();

完成

原文地址:https://www.cnblogs.com/crushxz/p/11330726.html

时间: 2024-08-25 08:50:30

vue之手把手教你写日历组件的相关文章

Android开发之手把手教你写ButterKnife框架(二)

欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52664112 本文出自:[余志强的博客] 上一篇博客Android开发之手把手教你写ButterKnife框架(一)我们讲了ButterKnife是什么.ButterKnife的作用和功能介绍以及ButterKnife的实现原理. 本篇博客主要讲在android studio中如何使用apt. 一.新建个项目, 然后创建一个module名叫processor 新建m

手把手教你写Sublime中的Snippet

手把手教你写Sublime中的Snippet Sublime Text号称最性感的编辑器, 并且越来越多人使用, 美观, 高效 关于如何使用Sublime text可以参考我的另一篇文章, 相信你会喜欢上的..Sublime Text 2使用心得 现在介绍一下Snippet, Snippets are smart templates that will insert text for you and adapt it to their context. Snippet 是插入到文本中的智能模板并

手把手教你写专利申请书/怎样申请专利

手把手教你写专利申请书·怎样申请专利 摘要小前言(一)申请前的准备工作    1.申请前查询    2.其它方面的考虑    3.申请文件准备(二)填写专利申请系列文档    1.实际操作步骤    2.详细操作    3.经验分享.注意事项(三)关于费用(四)其它的话參考资源提示常见问题的问与答 摘要: 怎样写好专利申请?由于非常多专利申请人都是第一次申请,因此,可能有一种神奇和些许恐惧.本文写的是怎样写专利申请书,手把手教你写专利申请并提供申请专利时的注意事项,专利申请费用及费用减缓等相关參

javascript动手写日历组件(2)——优化UI和添加交互(by vczero)

一.优化UI 继上一篇,http://www.cnblogs.com/vczero/p/js_ui_1.html.开始优化UI,主要优化的部分有: (1)增加星期行.(2)字体设置.(3)日期垂直居中.(4)将单元格->底部线条.(5)修改文本的颜色对比.(6)将内部调用的函数加前缀_,如_addHeader()._addWeekday(). 修改的后基本效果如下图: 整个代码做了小修小改: 1 var Calendar = function(div){ 2 this.div = documen

javascript动手写日历组件(1)——构建日历逻辑 (by vczero)

一.分析日历的组成部分和交互要素 (1)组成部分:选择年月部分.星期显示.包含本月(或者有前月和下一个月部分日子) (2)根据选择的年和月份,动态绘制日历面板. (3)一个日历 7(天) * 5(周) = 35格表格. (4)一个月份是统一的一个面板:一个月的头一天一定在日历面板的第一行,根据该天的“星期几”确定位置. (5)第一格子是星期一,最后一个格子是星期日,为5周的日历面板. 二.确定逻辑设计 日历上面的日历,8月1号建军节为什么会出现在这一格?因为一个月的天数是小于5周(35天)的,因

javascript动手写日历组件(3)——内存和性能优化(by vczero)

一.列表 javascript动手写日历组件的文章列表,主要是通过原生的JavaScript写的一个简约的日历组件: (1)javascript动手写日历组件(1)——构建日历逻辑:http://www.cnblogs.com/vczero/p/js_ui_1.html (2)javascript动手写日历组件(2)——优化UI和添加交互:http://www.cnblogs.com/vczero/p/js_ui_2.html (3)javascript动手写日历组件(2)——内存和性能优化:h

手把手教你写Windows 64位平台调试器

本文网页排版有些差,已上传了doc,可以下载阅读.本文中的所有代码已打包,下载地址在此. -------------------------------------------------------------------------------------------------------------------------------------------------------------- 手写一个调试器有助于我们理解hook.进程注入等底层黑客技术具体实现,在编写过程中需要涉及大

手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取

系列教材: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 看完两篇,相信大家已经从开始的小菜鸟晋升为中级菜鸟了,好了,那我们就继续我们的爬虫课程. 上一课呢一定是因为对手太强,导致我们并没有完整的完成尚妆网的爬虫. 吭吭~,我们这一课继续,争取彻底搞定尚妆网,不留任何遗憾. 我们先回顾一下,上一课主要遗留了两个问题,两个问题都和ajax有关. 1.由于是ajax加载下一页,导致下一页url并不会被系统自动发现. 2.商品页面的价格是通过a

手把手教你写电商爬虫-第四课 淘宝网商品爬虫自动JS渲染

系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取 老规矩,爬之前首先感谢淘宝公布出这么多有价值的数据,才让我们这些爬虫们有东西可以搜集啊,不过淘宝就不用我来安利了 广大剁手党相信睡觉的时候都能把网址打出来吧. 工欲善其事,必先利其器,先上工具: 1.神箭手云爬虫,2.Chrome浏览器 3.Chrome的插件XpathHelper 不知道是干嘛的同学请移步第一课