效果图:

代码:  

 

<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

最新文章

  1. BUG等级和严重等级关系
  2. Revit利用对正工具快速修改风管对齐方式
  3. iOS开发过程中,触控板的使用技巧
  4. slf4j,log4j,logback 初步使用
  5. JQ+rotate插件实现图片旋转,兼容IE7+ \ CHROME等浏览器
  6. mac版photoshop滤镜库报错解法
  7. SQLServer 2008以上误操作数据库恢复方法——日志尾部备份(转)
  8. 我的第一篇Markdown博客
  9. Sybase数据库截断和清空日志的方法
  10. oracle 表空间不足解决办法
  11. 命令行界面的C/S聊天室应用 (Socket多线程实现)
  12. Python7 - 面向对象编程进阶
  13. C#解析&quot;a=1&amp;b=2&amp;c=3&quot;字符串,微信支付返回字符串,替换&lt;br&gt;为&amp;
  14. [CSL 的魔法][求排序最少交换次数]
  15. Netty入门(1) - 简介
  16. Java 输入/输出——处理流(BufferedStream、PrintStream、转换流、推回输入流)
  17. 2019.1.3 WLAN 802.11 a/b/g PHY Specification and EDVT Measurement II - Transmit Spectrum Mask &amp; Current Consumption
  18. redis——队列
  19. 在Asp.net Core中使用中间件来管理websocket
  20. C++获取某个文件夹下的所有文件

热门文章

  1. React学习小结(一)
  2. InnoDB 锁的类型
  3. async-validator 源码学习笔记(三):rule
  4. Redhat7 安装 yum源(亲测有效)
  5. CVE-2017-8759 微软word漏洞复现以及利用
  6. STL基本用法的一些记录
  7. 从零开始,开发一个 Web Office 套件(13):删除、替换已选中文字
  8. 《前端运维》五、k8s--1安装与基本配置
  9. Apache Tomcat如何高并发处理请求
  10. XML的解析方式有哪几种?有什么区别?