vue.js实现日历插件使用方法详解
作者:laihaodong 时间:2024-05-13 09:38:43
今天要实现的功能就是以下这个功能:vue.js模拟日历插件
好了废话不多说了 直接上代码了
css:
*{
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#app{
width: 1000px;
margin: 10px auto;
}
.calender{
width: 1000px;
}
.calender table{
width: 1000px;
}
.calender table,th,tr,td{
border:1px solid #333333;
border-collapse: collapse;
}
.calender td{
height: 100px;
vertical-align: top;
text-align: left;
padding: 5px 0 0 5px;
font-size: 13px;
}
.calender td.cur{
color:red;
}
html:
<div id="app">
<div class="calender">
<table>
<caption>
<select v-model.number="year">
<option v-for="i of 490">{{i+1969}}</option>
</select>
<select v-model.number="month">
<option v-for="i of 12">{{i}}</option>
</select>
</caption>
<thead>
<tr>
<th>周日</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
</tr>
</thead>
<tbody>
<!--index 从0开始 i从1开始-->
<tr v-for="(a,index) of calender.length / 7" >
<td v-for="i of 7" :class="{cur:calender[index * 7 + (i - 1)].cur }">{{calender[index * 7 + (i - 1)].fullDay}}</td>
</tr>
</tbody>
</table>
</div>
</div>
js:
var vm = new Vue({
el:'#app',
data:{
year:2018,
month:1
},
computed:{
calender(){
var arr = [];
//new data 有三个参数: 1,年 2.月 3.默认是1 如果是0,表示上个月最后一天 - 前两天 3 后天
var nowMonthLength = new Date(this.year,this.month,0).getDate();
var nowMonthFirstWeek = new Date(this.year,this.month-1).getDay();
var lastMonthLength = new Date(this.year,this.month-1,0).getDate();
console.log('本月有:'+nowMonthLength);
console.log('本月第一天'+nowMonthFirstWeek);
console.log('上个月长度'+lastMonthLength);
// this.month = parseInt(this.month);
//每个月的上一个月是哪一年的那一个月
var pmonth = this.month == 1 ? 12 : this.month - 1;
//上一年
var pyear = this.month == 1 ? this.year - 1 :this.year;
//下一月
var nmonth = this.month == 12 ? 1 : this.month + 1;
//下一月
var nyear = this.month == 12 ? this.year + 1 : this.year;
//补零函数
// function toTwo(n) {
// return n < 10 ? '0' + n : n;
// }
function buling(n) {
return n.toString().length > 1 ? n.toString() : '0' + n.toString();
}
// 补充上个月的最后几天
while(nowMonthFirstWeek--){
arr.unshift({
day:lastMonthLength,
cur:true,
fullDay:`${pyear}-${buling(pmonth)}-${buling(lastMonthLength)}`
});
lastMonthLength--
}
console.log(arr);
//本月天数
var _a = 1;
while(nowMonthLength--){
arr.push({
day:_a,
cur:false,
fullDay:`${this.year}-${buling(this.month)}-${buling(_a)}`
});
_a++
}
//下个月补全
var nextLength = arr.length > 35 ? 42 - arr.length : 35 - arr.length;
_a = 1;
while (nextLength--){
arr.push({
day:_a,
cur:true,
fullDay:`${nyear}-${buling(nmonth)}-${buling(_a)}`
});
_a++
}
return arr;
}
}
})
注意:需要先引入你本地的vue.js文件, 才能正常运行哦!!!
来源:https://blog.csdn.net/laihaodong/article/details/82499984
标签:vue.js,日历插件
0
投稿
猜你喜欢
C#向数据库中插入或更新null空值与延迟加载lazy
2024-01-24 09:28:20
SQL Server 2005/2008 导入导出数据常见报错解决方法
2024-01-28 00:31:12
Python第三方库xlrd/xlwt的安装与读写Excel表格
2023-12-16 22:52:34
Django之Mode的外键自关联和引用未定义的Model方法
2023-09-04 13:34:40
使用python实现飞机大战游戏
2021-05-11 12:02:16
Mysql合并结果接横向拼接字段的实现步骤
2024-01-19 13:39:35
Python字体反爬实战案例分享
2021-06-18 01:00:46
Mybatis应用mysql存储过程查询数据实例
2024-01-16 09:05:52
Python使用cx_Freeze库生成msi格式安装文件的方法
2023-02-05 12:26:16
Python 实现两个服务器之间文件的上传方法
2022-04-22 10:32:03
对Python 数组的切片操作详解
2022-06-09 23:29:39
Pycharm 使用 Pipenv 新建的虚拟环境(图文详解)
2023-12-08 18:55:11
详解PyTorch手写数字识别(MNIST数据集)
2023-01-28 19:40:47
javascript图片预加载
2009-08-30 12:47:00
tensorflow-gpu2.3版本安装步骤
2022-11-25 09:35:40
Vue.2.0.5过渡效果使用技巧
2024-04-10 10:33:12
高效地获取XMLhttp对象
2010-01-19 13:49:00
SQL Server 公用表表达式(CTE)实现递归的方法
2024-01-26 15:20:10
python机器人行走步数问题的解决
2023-12-24 23:26:05
Python实现线性插值和三次样条插值的示例代码
2023-12-04 19:19:42