<!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实现简单日历的更多相关文章

  1. 基于Vue的简单日历组件

    日历组件 由于移动端项目中需要用到日历组件,网上找了下,没看到几个合适的,就尝试着自己写一个.然后发现也不是很复杂,目前只做了最基本的功能,大家也可以拿去做做二次开发. 如何写一个日历组件 基础效果如 ...

  2. js编写当天简单日历

    之前一直很想用javascript写一个日历,但是因为完全没有好的思路, 所以迟迟没有尝试.最近在网上刚好看到用javascript编写的简单日历的例子,代码量虽然不大, 但是我觉得很好地阐述了js日 ...

  3. js超简单日历

    用原生js写了一个超级简单的日历.当做是练习js中的Date类型. 思路: 获取某个日期,根据年份计算出每个月的天数. 利用Date中的getDay()知道该月份的第一天为星期几. 循环创建表格,显示 ...

  4. react构建淘票票webapp,及react与vue的简单比较。

    前言 前段时间使用vue2.0构建了淘票票页面,并写了一篇相关文章vue2.0构建淘票票webapp,得到了很多童鞋的支持,因此这些天又使用react重构了下这个项目,目的无他,只为了学习和共同进步! ...

  5. vue.js之生命周期,防止闪烁,计算属性的使用,vue实例简单方法和循环重复数据

    摘要:今天是比较糟糕的一天没怎么学习,原因是学校的wifi连不上了~~.今天学习一下vue的生命周期,如何防止闪烁(也就是用户看得到花括号),计算属性的使用,vue实例简单方法,以及当有重复数据时如何 ...

  6. JS写一个简单日历

    JS写一个日历,配合jQuery操作DOM <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  7. Vue的简单入门

    Vue的简单入门 一.什么是Vue? vue.js也一个渐进式JavaScript框架,可以独立完成前后端分离式web项目 渐进式:vue可以从小到控制页面中的一个变量后到页面中一块内容再到整个页面, ...

  8. vue教程2-04 vue实例简单方法

    vue教程2-04 vue实例简单方法 vue实例简单方法: vm.$el -> 就是元素 vm.$data -> 就是data <!DOCTYPE html> <htm ...

  9. 沉淀,再出发:VUE的简单理解

    沉淀,再出发:VUE的简单理解 一.前言 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过 ...

随机推荐

  1. 【cf842D】Vitya and Strange Lesson(01字典树)

    D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...

  2. Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)

    第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...

  3. 给 Haproxy 创建日志文件

    背景介绍:默认下的Haproxy配置是不会生成日志文件的,而无运行日志,无法确定系统运行是否流畅,无法提起预判可能发生的故障 创建Haproxy日志文件的步骤如下vi /etc/rsyslog.con ...

  4. HNOI2017 抛硬币 (FakeBeng)

    除了队长快跑外最难的题吧. 除了需要写\(exLucas\)之外,还教会了我大量的卡常技巧. 首先\(70\)分就是个直接按题意模拟,易得\(ans=\sum_{j=0}^{b} C_{b}^{j}\ ...

  5. 如何在TableLayout中均匀拉伸columns?

    本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术.本文主要解决columns难以均匀拉伸的问题,不 ...

  6. 【BZOJ2034】最大收益(贪心)

    [BZOJ2034]最大收益(贪心) 题面 BZOJ 题解 首先显然让价值越大的占用一个时刻一定更优. 所以把所有东西按照价值排序之后来处理,那么显然就是把前面的全部放好之后,考虑来放当前这个东西,如 ...

  7. layer 弹出层

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. A1046. Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  9. echarts如何给柱形图的每个柱子设置不同颜色

    总结下这几日用echarts库作基本图形遇到的一些问题. echarts快速上手可参考官网: http://echarts.baidu.com/tutorial.html#5%20%E5%88%86% ...

  10. (BFS) leetcode 690. Employee Importance

    690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...