vue日历(纯 js,没用任何插件和组件)
效果图:

代码:
<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,没用任何插件和组件)的更多相关文章
- 纯js客服插件集qq、旺旺、skype、百度hi、msn
原文 纯js客服插件集qq.旺旺.skype.百度hi.msn 客服插件,集qq.旺旺.skype.百度hi.msn 等 即时通讯工具,并可自己添加支持的通讯工具,极简主义,用法自己琢磨.我的博客 h ...
- 纯js的统计图插件-统计图
第一次写博客,写的不到望大家见谅! 今天给大家分享一个纯js的插件(统计图),有知道的可以在下面评论一起谈论一下,刚学着的时候,我是看了好久才看懂的一个基本结构,到后来我才知道原来直接去原网站上找到复 ...
- kPagination纯js实现分页插件
kPagination分页插件 纯js分页插件,压缩版本~4kb,样式可以自定义 demo 使用方法 <div id="pagination"></div> ...
- js+vue、纯js 按条件分页
听说大牛都从博客开始的... 人狠话不多,翠花上酸菜代码: 有注解基本上都看的懂!但是自己还是要注意以下几点,免得以后再浪费时间. #.vue 中监听事件 v-on:change=“vueChange ...
- vue 和 react 常用包(插件、组件 或 工具)
vue 和 react 都可以使用的包(只是 纯 js 功能的包) 1.qs : https://blog.csdn.net/sansan_7957/article/details/82227040 ...
- JS图形化插件利器组件系列 —— Gojs组件
阅读目录 一.组件效果预览 二.初次接触 1.Gojs简介 2.使用入门 三.综合效果 1.自定义流程的使用 2.工业流程图 四.总结 正文 前言:之前分享过两篇关于流程画图的前端组件,使用的jsPl ...
- baguetteBox.js响应式画廊插件(纯JS)
baguetteBox.js baguetteBox.js 是一个简单和易于使用lightbox纯JavaScript脚本,拥有图像放大缩小并带有相应的CSS3过度,并能在触摸屏等设备上完美展示. D ...
- vue.js国际化vue-i18n插件的使用问题,在模版文本、组件方法、jsf方法里的使用
vue.js国际化vue-i18n插件的使用问题,在模版文本.组件方法.jsf方法里的使用 1.在文本里使用{{$t("xxx")}} <span>{{$t(" ...
- 纯js实现复制到剪贴板功能
在网页上复制文本到剪切板,一般是使用JS+Flash结合的方法,网上有很多相关文章介绍.随着 HTML5 技术的发展,Flash 已经在很多场合不适用了,甚至被屏蔽.本文介绍的一款JS插件,实现了纯J ...
随机推荐
- PHP 的网站主要攻击方式有哪些?
1.命令注入(Command Injection)2.eval 注入(Eval Injection)3.客户端脚本攻击(Script Insertion)4.跨网站脚本攻击(Cross Site Sc ...
- php 23种设计模式 - 命令模式
命令模式 将一个请求封装为一个对象,从而使用户可用不同的请求对客户进行参数化.对请求排队或记录请求日志,以及支持撤销的操作. 命令模式以松散耦合主题为基础,发送消息.命令和请求,或通过一组处理程序发送 ...
- php 生成唯一订单号5种方法
第一种 private function doCreateOrderNumber($time){ $i=1; $dd = date('Ymd',$time); $aa = 'OH'.$dd; $res ...
- 全面解读 AWS Private 5G 的革新理念
目录 目录 目录 前言 近几年 AWS 在 5G ICT 领域的部署 AWS 与 Verizon 合作推出的 Private MEC 解决方案 AWS 与 Vodafone Business 合作推出 ...
- Kubernetes系列(五) Ingress
作者: LemonNan 原文地址: https://juejin.im/post/6878269825639317517 Kubernetes 系列 Kubernetes系列(一) Pod Kube ...
- CVE-2021-3129:Laravel远程代码漏洞复现分析
摘要:本文主要为大家带来CVE-2021-3129漏洞复现分析,为大家在日常工作中提供帮助. 本文分享自华为云社区<CVE-2021-3129 分析>,作者:Xuuuu . CVE-202 ...
- [链表]LeetCode 25 K组一个翻转链表
LeetCode 25 k组一个翻转链表 TITLE 示例 1: 输入:head = [1,2,3,4,5], k = 2 输出:[2,1,4,3,5] 示例 2: 输入:head = [1,2,3, ...
- JS 邮箱的验证(正则)
/^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/
- Mysql查询优化器之关于子查询的优化
下面这些sql都含有子查询: mysql> select * from t1 where a in (select a from t2); mysql> select * from (se ...
- elasticsearch 索引数据多了怎么办,如何调优,部署 ?
面试官:想了解大数据量的运维能力. 解答:索引数据的规划,应在前期做好规划,正所谓"设计先行,编码在后", 这样才能有效的避免突如其来的数据激增导致集群处理能力不足引发的线上客户 ...