效果图:

代码:  

 

<template>
<div class="calender">
<div class="top">
<div class="top_date">
{{year}}年{{month}}月
</div>
<div class="btn_wrap">
<ul>
<li @click="handleShowNextMonth">
下个月
</li>
<li @click="handleShowToday">
今天
</li>
<li @click="handleShowLastMonth">
上个月
</li>
</ul>
</div>
</div>
<div class="date_wrap">
<ul class="week">
<li>日</li>
<li>一</li>
<li>二</li>
<li>三</li>
<li>四</li>
<li>五</li>
<li>六</li>
</ul>
<ul class="day">
<li v-for="(item,index) in days" :key=index :class="{now:nowLi==year.toString()+month.toString()+item}">
{{item}}
</li>
</ul>
</div>
</div>
</template> <script>
export default {
name: 'calender',
data () {
return {
year:'',
month:'',
days:[],
nowLi:'',
}
},
methods:{
//控制当前日期显示特殊样式
handleShowDateStyle(){
let now = new Date()
this.nowLi=now.getFullYear().toString()+(now.getMonth()+1).toString()+now.getDate().toString()
console.log(this.nowLi)
},
//得到当前年这个月分有多少天
getDays(Y,M){
let day = new Date(Y, M, 0).getDate()
return day;
},
//得到当前年,这个月的一号是周几
getWeek(Y,M){
let now = new Date()
now.setFullYear(this.year)
now.setMonth(this.month-1)
now.setDate(1);
let week = now.getDay();
return week;
},
pushDays(){
//将这个月多少天加入数组days
for(let i = 1; i<=this.getDays(this.year,this.month);i++){
this.days.push(i)
}
//将下个月要显示的天数加入days
// for(let i = 1;i<=42-this.getDays(this.year,this.month)-this.getWeek(this.year,this.month);i++){
// this.days.push(i)
// }
//将上个月要显示的天数加入days
for(let i=0;i<this.getWeek(this.year,this.month);i++){
var lastMonthDays=this.getDays(this.year,this.month-1)
this.days.unshift(lastMonthDays-i)
}
console.log(this.days)
console.log(this.getWeek(this.year,this.month))
},
getDate(){
let now = new Date();
this.year = now.getFullYear();
this.month = now.getMonth()+1;
this.pushDays(); },
changeDate(){ },
handleShowNextMonth(){
this.days=[];
if(this.month<12){
this.month=this.month+1;
this.pushDays();
}else{
this.month= this.month=1;
this.year=this.year+1;
this.pushDays();
} },
handleShowToday(){
this.days=[];
let now = new Date();
this.year=now.getFullYear();
this.month=now.getMonth()+1;
this.pushDays();
},
handleShowLastMonth(){
this.days=[];
if(this.month>1){
this.month=this.month-1;
this.pushDays();
}else if( this.year>1970){
this.month=12;
this.year=this.year-1;
this.pushDays();
}else{
alert("不能查找更远的日期")
} }
},
mounted(){
this.getDate();
this.handleShowDateStyle();
}
}
</script>
<style scoped>
.calender{
width: 600px;
position: relative;
margin: 0 auto;
margin-top: 50px;
border: 1px solid #ddd;
padding: 20px;
}
.top{
width: 100%;
position: relative;
display: flex;
border-bottom: 1px solid #ddd;
padding-bottom: 20px;
}
.top_date{
width: 100px;
text-align: left;
line-height: 42px;
}
.btn_wrap{
flex: 1;
text-align: right
}
.btn_wrap ul{
display: flex;
flex-direction: row-reverse
}
.btn_wrap ul li{
padding: 10px 20px;
border: 1px solid #ddd;
font-size: 14px;
line-height: 20px;
cursor: pointer;
}
.btn_wrap ul li:hover{
background: #ddd;
color:red;
}
.btn_wrap ul li:first-child{
border-left: none;
}
.btn_wrap ul li:last-child{
border-right: none;
}
.date_wrap{
position: relative;
}
.week{
display: flex;
flex-direction: row;
padding: 20px;
font-size: 16px;
}
.week li{
width: 14.28%;
}
.day{
display: flex;
flex-direction: row;
padding: 20px;
font-size: 16px;
flex-wrap: wrap;
}
.day li{
width: 14.28%;
padding: 20px;
box-sizing: border-box;
border: 1px solid #ddd
}
.day li:nth-child(n+8){
border-top:none;
}
.day li:nth-child(n+1){
border-right: none;
}
.day li:nth-child(7n){
border-right: 1px solid #ddd
}
.now{
background: #f2f8fe;
color:#1989fa;
} </style>

感谢 各位老板  小额打赏:   (有问题call俺)

参考链接:https://www.jianshu.com/p/6d8a138d2592

vue日历(纯 js,没用任何插件和组件)的更多相关文章

  1. 纯js客服插件集qq、旺旺、skype、百度hi、msn

    原文 纯js客服插件集qq.旺旺.skype.百度hi.msn 客服插件,集qq.旺旺.skype.百度hi.msn 等 即时通讯工具,并可自己添加支持的通讯工具,极简主义,用法自己琢磨.我的博客 h ...

  2. 纯js的统计图插件-统计图

    第一次写博客,写的不到望大家见谅! 今天给大家分享一个纯js的插件(统计图),有知道的可以在下面评论一起谈论一下,刚学着的时候,我是看了好久才看懂的一个基本结构,到后来我才知道原来直接去原网站上找到复 ...

  3. kPagination纯js实现分页插件

    kPagination分页插件 纯js分页插件,压缩版本~4kb,样式可以自定义 demo 使用方法 <div id="pagination"></div> ...

  4. js+vue、纯js 按条件分页

    听说大牛都从博客开始的... 人狠话不多,翠花上酸菜代码: 有注解基本上都看的懂!但是自己还是要注意以下几点,免得以后再浪费时间. #.vue 中监听事件 v-on:change=“vueChange ...

  5. vue 和 react 常用包(插件、组件 或 工具)

    vue 和 react  都可以使用的包(只是 纯 js 功能的包) 1.qs : https://blog.csdn.net/sansan_7957/article/details/82227040 ...

  6. JS图形化插件利器组件系列 —— Gojs组件

    阅读目录 一.组件效果预览 二.初次接触 1.Gojs简介 2.使用入门 三.综合效果 1.自定义流程的使用 2.工业流程图 四.总结 正文 前言:之前分享过两篇关于流程画图的前端组件,使用的jsPl ...

  7. baguetteBox.js响应式画廊插件(纯JS)

    baguetteBox.js baguetteBox.js 是一个简单和易于使用lightbox纯JavaScript脚本,拥有图像放大缩小并带有相应的CSS3过度,并能在触摸屏等设备上完美展示. D ...

  8. vue.js国际化vue-i18n插件的使用问题,在模版文本、组件方法、jsf方法里的使用

    vue.js国际化vue-i18n插件的使用问题,在模版文本.组件方法.jsf方法里的使用 1.在文本里使用{{$t("xxx")}} <span>{{$t(" ...

  9. 纯js实现复制到剪贴板功能

    在网页上复制文本到剪切板,一般是使用JS+Flash结合的方法,网上有很多相关文章介绍.随着 HTML5 技术的发展,Flash 已经在很多场合不适用了,甚至被屏蔽.本文介绍的一款JS插件,实现了纯J ...

随机推荐

  1. 【一周聚焦】 联邦学习 arxiv 2.16-3.10

    这是一个新开的每周六定期更新栏目,将本周arxiv上新出的联邦学习等感兴趣方向的文章进行总结.与之前精读文章不同,本栏目只会简要总结其研究内容.解决方法与效果.这篇作为栏目首发,可能不止本周内容(毕竟 ...

  2. 如何构建一个PKM系统

    原文链接 这篇文章是个人知识总结相关的第一篇文章,主要目的是为了阐述我们为什么需要一个 PKM 系统,并且简单对自己这几年亲身形成的PKM 系统进行总结. PKM 是什么? PKM 是 Persona ...

  3. 解决vue单页面应用做微信JSSDK注入权限时出现“invalid signature”(ios端)

    --都说微信开发多坑,没想到遇到一个天坑. 在做一个vue项目时,要用到微信JS-SDK,官方文档详见:https://developers.weixin.qq.com/doc/offiaccount ...

  4. CF258D题解

    太厉害啦 首先做期望题最不能忘记的就是期望的线性性. 所以我们直接将全局逆序对对数拆成两个数其中一个比另一个大的期望(概率),设为 \(f[i][j]\),初值为 \([a_i>b_j]\). ...

  5. springsecurity-01-0511

    springsecurity-01-0511课堂代码 BaseController package com.springsecurity.springsecurity.controller; impo ...

  6. 七天接手react项目 系列 —— react 路由

    其他章节请看: 七天接手react项目 系列 react 路由 本篇首先讲解路由原理,接着以一个基础路由示例为起点讲述路由最基础的知识,然后讲解嵌套路由.路由传参,最后讲解路由组件和一般组件的区别,以 ...

  7. spinlock 设计的初衷,当只有单核时是否还需要锁

    自旋锁,的设计初衷是什么,是为了解决什么问题.如果只有一个cpu,并且是单核,那是否还需要用到自旋锁.

  8. 网络监听HTTP协议信息实验

    一.开启环境 登录web服务器,在服务器中开启phpstudy服务器环境. 在操作机中打开目标站[Web服务器IP地址]地址.安装wordpress,数据库名:wordpress,用户名root 密码 ...

  9. MVC 生成安全验证码(例:用于登陆验证) 方法2

    MVC前台页面中,重新获取图片验证码的第二种方式:(前端页面代码如下,后台页面请参考上一篇文章) ---------html <td> <img id="imgValida ...

  10. C语言对Mysql函数操作

    数据类型 MYSQL MYSQL结构代表一个数据库连接句柄,包含有关服务器的连接状态的信息,几乎所有函数都是用到它 typedef struct st_mysql { NET net; /* Comm ...