vue实现简单日历

摘要:
15} 1617#日历{18width;22box shadow;33}3435。标题箭头{36padding;49padding;63background;87color;divide=“calendar”>98<divclass=“year-month”>span>{{currentYear}}}}年{currentMonth}月<
  1 <!DOCTYPE html>
  2 <html lang="en">
  3 
  4 <head>
  5     <meta charset="UTF-8">
  6     <title>日历</title>
  7     <style>
  8 * {
  9   padding: 0;
 10   margin: 0;
 11 }
 12 
 13 ul {
 14   list-style-type: none;
 15 }
 16 
 17 #calendar {
 18   width: 450px;
 19   height: 300px;
 20   margin: 100px auto;
 21   border-radius: 2px;
 22   box-shadow: 0 2px 1px -1px rgba(0, 0, 0, 0.2), 0 1px 1px 0 rgba(0, 0, 0, 0.14),
 23     0 1px 3px 0 rgba(0, 0, 0, 0.12);
 24 }
 25 
 26 .title {
 27   font-size: 20px;
 28   letter-spacing: 3px;
 29   display: flex;
 30   justify-content: space-between;
 31   background-color: #ed8079;
 32   color: #ffffff;
 33 }
 34 
 35 .title .arrow {
 36   padding: 20px;
 37   cursor: pointer;
 38 }
 39 
 40 .year-month {
 41   display: flex;
 42   flex-direction: column;
 43   align-items: center;
 44   justify-content: space-around;
 45 }
 46 
 47 .weekdays {
 48   margin: 0;
 49   padding: 10px 0;
 50   display: flex;
 51   flex-wrap: wrap;
 52   justify-content: space-around;
 53 }
 54 
 55 .weekdays li {
 56   display: inline-block;
 57   width: 13.6%;
 58   text-align: center;
 59 }
 60 
 61 .days {
 62   padding: 0;
 63   background: #ffffff;
 64   margin: 0;
 65   display: flex;
 66   flex-wrap: wrap;
 67   justify-content: space-around;
 68 }
 69 
 70 .days li {
 71   display: inline-block;
 72   width: 14.2%;
 73   text-align: center;
 74   padding-bottom: 8px;
 75   padding-top: 8px;
 76 }
 77 
 78 .days li .active {
 79   padding: 4px 10px;
 80   border-radius: 50%;
 81   background: #ed8079;
 82   color: #fff;
 83 }
 84 
 85 .days li .other {
 86   padding: 5px;
 87   color: gainsboro;
 88 }
 89 
 90 .days li:hover {
 91   background: #e1e1e1;
 92 }
 93 </style>
 94 </head>
 95 
 96 <body>
 97     <div id="calendar">
 98         <!-- 标题 -->
 99         <div class="title">
100             <div class="arrow" 
101             @click="changeMonth('pre')"
102             ></div>
103             <div class="year-month">
104                 <span>{{currentYear}}年{{currentMonth}}月</span>
105             </div>
106 
107             <div class="arrow"
108              @click="changeMonth('next')"
109              ></div>
110         </div>
111         <!-- 星期行 -->
112         <ul class="weekdays">
113             <li 
114             v-for='(val,key) in weeks' 
115             >
116             <span :style='chooseStyle(key)'>
117                 {{val}}
118             </span>
119 
120             </li>
121 
122         </ul>
123         <!-- 日期 -->
124         <ul class="days">
125             <li 
126             v-for='(val,key) in days'
127             >
128                 <span 
129                 :class='chooseClass(val.day)'
130                 >
131                 {{val.day.getDate()}}</span>
132             </li>
133         </ul>
134     </div>
135     <script src="../vue.js"></script>
136 <script>
137 new Vue({
138   el: "#calendar",
139   data: {
140     currentDay: 1,
141     currentMonth: 1,
142     currentYear: 1970,
143     currentWeek: 1,
144     weeks: ["", "", "", "", "", "", ""],
145     days: []
146   },
147   created() {
148     this.init();
149   },
150   methods: {
151     init(data) {
152       let day;
153 
154       if (data) {
155         day = new Date(data);
156       } else {
157         let now = new Date();
158         day = new Date(this.formDate(now.getFullYear(), now.getMonth() + 1, 1));
159       }
160 
161       this.currentDay = day.getDate();
162       this.currentYear = day.getFullYear();
163       this.currentMonth = day.getMonth() + 1;
164 
165       this.currentWeek = day.getDay();
166 
167       if (!this.currentWeek) {
168         this.currentWeek = 7;
169       }
170 
171       this.days.length = 0;
172       let str = this.formDate(
173         this.currentYear,
174         this.currentMonth,
175         this.currentDay
176       );
177 
178       for (let i = 2 - this.currentWeek ; i < 37 - this.currentWeek; i++) {
179         let d = new Date(str);
180         d.setDate(i);
181         this.days.push({
182           day: d
183         });
184       }
185 
186       // //获取上月末至本月第一天的日期
187       //   for (let i = this.currentWeek - 1; i >= 0; i--) {
188       //     let d = new Date(str);
189       //     d.setDate(d.getDate() - i);
190       //     this.days.push({
191       //       day: d
192       //     });
193       //   }
194 
195       // //获取剩余的日期
196       //   for (let i = 1; i <= 35 - this.currentWeek; i++) {
197       //     let d = new Date(str);
198       //     d.setDate(d.getDate() + i);
199       //     this.days.push({
200       //       day: d
201       //     });
202       //   }
203     },
204 
205     //其他月加class名'other',今天加class名'active'
206     chooseClass(day) {
207       if (day.getMonth() + 1 != this.currentMonth) return "other";
208 
209       let a = new Date() - day;
210       if (a > 0 && a < 86400000) return "active";
211     },
212     //改变周六日显示颜色
213     chooseStyle(key) {
214       if (key === 5 || key === 6) return "color:#f1aaab";
215     },
216     //切换月份
217     changeMonth(a) {
218       let d = new Date(this.formDate(this.currentYear, this.currentMonth, 1));
219 
220       // setDate(0); 上月最后一天
221       // setDate(-1); 上月倒数第二天
222       // setDate(n) 参数n为 上月最后一天的前后n天
223       a === "pre" ? d.setDate(0) : d.setDate(35);
224 
225       this.init(this.formDate(d.getFullYear(), d.getMonth() + 1, 1));
226     },
227     //返回字符串个格式的日期
228     formDate(year, month, day) {
229       return year + "-" + month + "-" + day;
230     }
231   }
232 });
233 </script>
234 </body>
238 </html>

免责声明:文章转载自《vue实现简单日历》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇控制台连接oracle11g报ORA-12560异常为ImageView设置背景图片(代码中)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Log4Net使用详解(续)

说明自从上次在2008年在博客上发表过有关log4net的用法介绍文章之后(网址:http://blog.csdn.net/zhoufoxcn/archive/2008/03/26/2220533.aspx),有不少朋友在博文下留言询问一些细节,现在就一些比较普遍的问题做一些稍微深入的解答,希望大家满意。 首先说明一点的是,log4net解决的问题是提供一...

[转帖]制作数据字典

  这部分内容和VB6的关系不大,但是确是困扰我的一个问题。 这几天在整理数据字典,以前的办法是用Excel来制作,一个一个的填写特别麻烦,制作到好说,关键就是一旦结果变更了,改来改去的麻烦死了。 后来改用数据库关系图来制作打印出来,看着还是那么回事情,但是表格的排序和查找太麻烦了,到底有没有好的办法呢? 当然有了,实际上在SqlServer中利用Sql语...

04 . Jenkins部署Java项目

配置基础环境 注意: 安装使用Jenkins和Gitlab请先看前面文章,此处不详细介绍 https://www.cnblogs.com/you-men/p/13126873.html Jenkins大多数情况下都是用来部署Java项目,Java项目有一个特点是需要编译和打包的,一般情况下编译和打包都是用maven完成,所以系统环境中需要安装maven。...

mybaits3.2.8 别名包扫描通配符

<mybatis.version>3.2.8</mybatis.version><mybatis.spring.version>1.2.2</mybatis.spring.version><mybatis.generator.version>1.3.2</mybatis.generator....

js动态设置根元素的rem方案

方案需求: rem 单位在做移动端的h5开发的时候是最经常使用的单位。为解决自适应的问题,我们需要动态的给文档的根节点添加font-size 值。 使用mediaquery 可以解决这个问题,但是每一个文件都引用一大串的font-size 值很繁琐,而且值也不能达到连续的效果。 就使用js动态计算给文档的fopnt-size 动态赋值解决问题。 设计稿以7...

从Prism中学习设计模式之MVVM 模式简述MVVM

从Prism中学习设计模式之MVVM 模式(一)--简述MVVM 在学习Prism中涉及的MVVM之前,我想有必要分别把MVC&MVP,MVVM,Command三种模式进行简单的总结,这样可以更好的理解Prism尤其是WPF的设计理念。 本文内容: MVVM的来龙去脉 为什么Prism使用MVVM 示例讲解 一、MVVM的来龙去脉      在...