vue实现简单日历
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>日历</title>
<style>
* {
padding: 0;
margin: 0;
} ul {
list-style-type: none;
} #calendar {
width: 450px;
height: 300px;
margin: 100px auto;
border-radius: 2px;
box-shadow: 0 2px 1px -1px rgba(0, 0, 0, 0.2), 0 1px 1px 0 rgba(0, 0, 0, 0.14),
0 1px 3px 0 rgba(0, 0, 0, 0.12);
} .title {
font-size: 20px;
letter-spacing: 3px;
display: flex;
justify-content: space-between;
background-color: #ed8079;
color: #ffffff;
} .title .arrow {
padding: 20px;
cursor: pointer;
} .year-month {
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
} .weekdays {
margin: 0;
padding: 10px 0;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
} .weekdays li {
display: inline-block;
width: 13.6%;
text-align: center;
} .days {
padding: 0;
background: #ffffff;
margin: 0;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
} .days li {
display: inline-block;
width: 14.2%;
text-align: center;
padding-bottom: 8px;
padding-top: 8px;
} .days li .active {
padding: 4px 10px;
border-radius: 50%;
background: #ed8079;
color: #fff;
} .days li .other {
padding: 5px;
color: gainsboro;
} .days li:hover {
background: #e1e1e1;
}
</style>
</head> <body>
<div id="calendar">
<!-- 标题 -->
<div class="title">
<div class="arrow"
@click="changeMonth('pre')"
> ❮ </div>
<div class="year-month">
<span>{{currentYear}}年{{currentMonth}}月</span>
</div> <div class="arrow"
@click="changeMonth('next')"
> ❯ </div>
</div>
<!-- 星期行 -->
<ul class="weekdays">
<li
v-for='(val,key) in weeks'
>
<span :style='chooseStyle(key)'>
{{val}}
</span> </li> </ul>
<!-- 日期 -->
<ul class="days">
<li
v-for='(val,key) in days'
>
<span
:class='chooseClass(val.day)'
>
{{val.day.getDate()}}</span>
</li>
</ul>
</div>
<script src="../vue.js"></script>
<script>
new Vue({
el: "#calendar",
data: {
currentDay: 1,
currentMonth: 1,
currentYear: 1970,
currentWeek: 1,
weeks: ["一", "二", "三", "四", "五", "六", "日"],
days: []
},
created() {
this.init();
},
methods: {
init(data) {
let day; if (data) {
day = new Date(data);
} else {
let now = new Date();
day = new Date(this.formDate(now.getFullYear(), now.getMonth() + 1, 1));
} this.currentDay = day.getDate();
this.currentYear = day.getFullYear();
this.currentMonth = day.getMonth() + 1; this.currentWeek = day.getDay(); if (!this.currentWeek) {
this.currentWeek = 7;
} this.days.length = 0;
let str = this.formDate(
this.currentYear,
this.currentMonth,
this.currentDay
); for (let i = 2 - this.currentWeek ; i < 37 - this.currentWeek; i++) {
let d = new Date(str);
d.setDate(i);
this.days.push({
day: d
});
} // //获取上月末至本月第一天的日期
// for (let i = this.currentWeek - 1; i >= 0; i--) {
// let d = new Date(str);
// d.setDate(d.getDate() - i);
// this.days.push({
// day: d
// });
// } // //获取剩余的日期
// for (let i = 1; i <= 35 - this.currentWeek; i++) {
// let d = new Date(str);
// d.setDate(d.getDate() + i);
// this.days.push({
// day: d
// });
// }
}, //其他月加class名'other',今天加class名'active'
chooseClass(day) {
if (day.getMonth() + 1 != this.currentMonth) return "other"; let a = new Date() - day;
if (a > 0 && a < 86400000) return "active";
},
//改变周六日显示颜色
chooseStyle(key) {
if (key === 5 || key === 6) return "color:#f1aaab";
},
//切换月份
changeMonth(a) {
let d = new Date(this.formDate(this.currentYear, this.currentMonth, 1)); // setDate(0); 上月最后一天
// setDate(-1); 上月倒数第二天
// setDate(n) 参数n为 上月最后一天的前后n天
a === "pre" ? d.setDate(0) : d.setDate(35); this.init(this.formDate(d.getFullYear(), d.getMonth() + 1, 1));
},
//返回字符串个格式的日期
formDate(year, month, day) {
return year + "-" + month + "-" + day;
}
}
});
</script>
</body>
</html>
vue实现简单日历的更多相关文章
- 基于Vue的简单日历组件
日历组件 由于移动端项目中需要用到日历组件,网上找了下,没看到几个合适的,就尝试着自己写一个.然后发现也不是很复杂,目前只做了最基本的功能,大家也可以拿去做做二次开发. 如何写一个日历组件 基础效果如 ...
- js编写当天简单日历
之前一直很想用javascript写一个日历,但是因为完全没有好的思路, 所以迟迟没有尝试.最近在网上刚好看到用javascript编写的简单日历的例子,代码量虽然不大, 但是我觉得很好地阐述了js日 ...
- js超简单日历
用原生js写了一个超级简单的日历.当做是练习js中的Date类型. 思路: 获取某个日期,根据年份计算出每个月的天数. 利用Date中的getDay()知道该月份的第一天为星期几. 循环创建表格,显示 ...
- react构建淘票票webapp,及react与vue的简单比较。
前言 前段时间使用vue2.0构建了淘票票页面,并写了一篇相关文章vue2.0构建淘票票webapp,得到了很多童鞋的支持,因此这些天又使用react重构了下这个项目,目的无他,只为了学习和共同进步! ...
- vue.js之生命周期,防止闪烁,计算属性的使用,vue实例简单方法和循环重复数据
摘要:今天是比较糟糕的一天没怎么学习,原因是学校的wifi连不上了~~.今天学习一下vue的生命周期,如何防止闪烁(也就是用户看得到花括号),计算属性的使用,vue实例简单方法,以及当有重复数据时如何 ...
- JS写一个简单日历
JS写一个日历,配合jQuery操作DOM <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- Vue的简单入门
Vue的简单入门 一.什么是Vue? vue.js也一个渐进式JavaScript框架,可以独立完成前后端分离式web项目 渐进式:vue可以从小到控制页面中的一个变量后到页面中一块内容再到整个页面, ...
- vue教程2-04 vue实例简单方法
vue教程2-04 vue实例简单方法 vue实例简单方法: vm.$el -> 就是元素 vm.$data -> 就是data <!DOCTYPE html> <htm ...
- 沉淀,再出发:VUE的简单理解
沉淀,再出发:VUE的简单理解 一.前言 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过 ...
随机推荐
- Ubuntu解压
tar -zxvf FileName.tar.gz tar -jxvf FileName.tar.bz2 unzip FileName.zip sudo dpkg -i 文件名.deb
- Codeforces Bubble Cup 11 J. Moonwalk challenge加强版
题意 有一棵 \(n\) 个节点的树,每条边上有一个字符,有 \(m\) 次询问. 每次会选定两个点 \(u, v\) , \(u\) 到 \(v\) 的路径上的字符形成了一个字符串 \(T\) ,再 ...
- hexo博客添加功能
设置Hexo主题模式 Hexo主题中,有三种不同的模式,通过切换模式,让NexT主题显示不一样的样式.在NexT根目录下有一个同样名称为_config.yml,为了区分hexo根目录下的_config ...
- https搭建实例
:(用的)https://www.coderecord.cn/lets-encrypt-wildcard-certificates.html :acme.shvim .acme.sh/account. ...
- Educational Codeforces Round 46 C - Covered Points Count
C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h&g ...
- A1140. Look-and-say Sequence
Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...
- 牛客网暑期ACM多校训练营 第九场
HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...
- c++ sort
老是搞混 return bool eg. bool cmp(node a,node b) { if (a.score==b.score) ; else return a.score>b.scor ...
- 斯坦福大学公开课机器学习:advice for applying machine learning | diagnosing bias vs. variance(机器学习:诊断偏差和方差问题)
当我们运行一个学习算法时,如果这个算法的表现不理想,那么有两种原因导致:要么偏差比较大.要么方差比较大.换句话说,要么是欠拟合.要么是过拟合.那么这两种情况,哪个和偏差有关.哪个和方差有关,或者是不是 ...
- ThymeLeaf的eclipse插件安装
“Help”----“Install New Software...” 输入: http://www.thymeleaf.org/eclipse-plugin-update-site/ 一路Next, ...