vue实现日历

  之前在上家公司做过一个公司人员考勤的东西,里面需要用到日历,当时自己用vue随便写了一个,比较简单

  下面代码是删掉了其他功能的代码,只留下日历部分

  1. <template>
  2. <div class="lookForMonth_wrapper">
  3. <div class="lookForMonth_top">
  4. <div class="selectDate">
  5. <div>{{year}} 年 {{month}} 月</div>
  6. <div class="upDownSelect">
  7. <div class="upDownSelect_item" @click="dateUp"></div>
  8. <div class="upDownSelect_item" @click="dateDown"></div>
  9. </div>
  10. </div>
  11. </div>
  12. <div class="calendar" :style="calendarStyle">
  13. <div v-for="(item,index) in calendarData" class="calendar_item" :key='index' :class="{ash:item.color==='ash',date:index>6&&item.color!=='ash'}">
  14. <p class="dateEdit">{{item.label}}<i class="el-icon-edit-outline" v-if="item.color!=='ash'&&index>=7"></i></p>
  15. <p v-if='index>6'>上班</p> // 打工人
  16. </div>
  17. </div>
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. name: "lookForMonth",
  23. data: () => {
  24. return {
  25. calendarData: [{label:"日"},{label: "一"}, {label:"二"},{label: "三"},{label: "四"},{label: "五"},{label: "六"}], //日历循环渲染数据
  26. year: 0, //当前日期年
  27. month: 0, //当前日期月数
  28. date: 0, //当前日期号数
  29. day: -1, //当前星期几
  30. };
  31. },
  32. filters:{
  33. },
  34. computed: {
  35. // 根据当月日期详情更改日历表格高度
  36. calendarStyle() {
  37. if (this.calendarData.length > 42) {
  38. return "height: 701px;";
  39. } else {
  40. return "height: 601px;";
  41. }
  42. }
  43. },
  44. async created(){
  45. // 获取当前日期数据
  46. this.getNow();
  47. // 获取当前月份一号的时间戳
  48. let firstTime = +new Date(this.year,this.month-1,1,0,0,0)
  49. this.getCalendarDate(); // 给calendarData添加当月数据
  50. },
  51. mounted() {
  52. },
  53. methods: {
  54. // 获取当前时间
  55. getNow() {
  56. let now = new Date()
  57. this.year = +now.getFullYear()
  58. this.month = +now.getMonth() + 1
  59. this.date = +now.getDate()
  60. this.day = +now.getDay()
  61. },
  62. // 获取每个月的天数
  63. monthDay(month) {
  64. if ([1,3,5,7,8,10,12].includes(month)) {
  65. return 31
  66. } else if ([4,6,9,11].includes(month)) {
  67. return 30
  68. } else if (month === 2) {
  69. // 判断当年是否为闰年
  70. if (
  71. (this.year % 4 === 0 && this.year % 100 !== 0) ||
  72. this.year % 400 === 0
  73. ) {
  74. return 29
  75. } else {
  76. return 28
  77. }
  78. }
  79. },
  80. // 给calendarData添加当月数据
  81. getCalendarDate() {
  82. // 获取当前月份一号星期几
  83. let firstDay = new Date(this.year + "-" + this.month + "-" + "01").getDay();
  84. this.calendarData = [{label:"日"},{label: "一"}, {label:"二"},{label: "三"},{label: "四"},{label: "五"},{label: "六"}];
  85. let num = parseInt(firstDay);
  86. let nowDays = this.monthDay(this.month);
  87. let lastMonth = this.month - 1>0?this.month - 1:12;
  88. let lastDays = this.monthDay(lastMonth);
  89. // 循环添加上一个月数据
  90. for (let i = 0; i < num; i++) {
  91. this.calendarData.push({label:lastDays - num + i + 1,color:'ash'});
  92. }
  93. // 循环添加当月数据
  94. for (let i = 0; i < nowDays; i++) {
  95. this.calendarData.push({label:i + 1});
  96. }
  97. // 循环添加下一个月数据
  98. if (this.calendarData.length % 7 !== 0) {
  99. let surplusDay = 7 - (this.calendarData.length % 7);
  100. for (let i = 0; i < surplusDay; i++) {
  101. this.calendarData.push({label:i + 1,color:'ash'});
  102. }
  103. }
  104. this.loading = false
  105. },
  106. // 将日期调上
  107. dateUp() {
  108. this.month--;
  109. if (this.month <= 0) {
  110. this.year--;
  111. this.month = 12;
  112. }
  113. this.getCalendarDate(); // 给calendarData添加当月数据
  114. },
  115. // 将日期调下
  116. dateDown() {
  117. this.month++;
  118. if (this.month > 12) {
  119. this.year++;
  120. this.month = 1;
  121. }
  122. this.getCalendarDate(); // 给calendarData添加当月数据
  123. },
  124. }
  125. };
  126. </script>
  127. <style lang="scss" scoped>
  128. .lookForMonth_wrapper {
  129. padding: 20px;
  130. width: 701px;
  131. margin: auto;
  132. }
  133. .lookForMonth_top {
  134. margin-bottom: 20px;
  135. overflow: hidden;
  136. .selectTeacher {
  137. float: left;
  138. }
  139. .selectDate {
  140. height: 30px;
  141. line-height: 30px;
  142. float: right;
  143. display: flex;
  144. .upDownSelect {
  145. display: flex;
  146. flex-direction: column;
  147. margin-top: -2px;
  148. margin-left: 5px;
  149. .upDownSelect_item {
  150. width: 0;
  151. height: 0;
  152. border: 7px solid transparent;
  153. cursor: pointer;
  154. }
  155. .upDownSelect_item:nth-child(1) {
  156. border-bottom: 7px solid #666;
  157. margin-bottom: 5px;
  158. &:hover {
  159. border-bottom: 7px solid skyblue;
  160. }
  161. }
  162. .upDownSelect_item:nth-child(2) {
  163. border-top: 7px solid #666;
  164. &:hover {
  165. border-top: 7px solid skyblue;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. /* 日历表样式=======================================↓ */
  172. .calendar {
  173. width: 701px;
  174. border-top: 1px solid #ccc;
  175. border-left: 1px solid #ccc;
  176. display: flex;
  177. flex-wrap: wrap;
  178. box-sizing: border-box;
  179. .calendar_item {
  180. box-sizing: border-box;
  181. width: 100px;
  182. height: 100px;
  183. border-right: 1px solid #ccc;
  184. border-bottom: 1px solid #ccc;
  185. text-align: center;
  186. display: flex;
  187. flex-direction: column;
  188. justify-content: center;
  189. align-items: center;
  190. &.date:hover{
  191. background: #eee;
  192. }
  193. .status{
  194. margin-top: 10px;
  195. &.textBlue{
  196. color: blue;
  197. }
  198. &.textRed{
  199. color: brown;
  200. }
  201. }
  202. .el-icon-edit-outline{
  203. cursor: pointer;
  204. margin-left: 7px;
  205. }
  206. }
  207. .ash{
  208. color: gainsboro;
  209. }
  210. .dateEdit{
  211. margin-bottom: 10px;
  212. }
  213. }
  214. </style>

效果如下:

vue实现日历的更多相关文章

  1. Vue自定义日历组件

    今天给大家介绍Vue的日历组件,可自定义样式.日历类型及支持扩展,可自定义事件回调.Props数据传输. 线上demo效果 示例 Template: <Calendar :sundayStart ...

  2. 使用ant design vue的日历组件,实现一个简单交易日与非交易日的切换

    使用ant design vue的日历组件,实现一个简单交易日与非交易日的切换 需求: 日历区分交易日.非交易日 可以切换面板查看整年交易日信息 可以在手动调整交易日.非交易日 演示实例 序--使用软 ...

  3. vue初学实践之路——vue简单日历组件(1)

    ---恢复内容开始--- 最近做的项目有一个需求,需要有一个日历组件供预定功能使用,之前的代码过于繁琐复杂,所以我采用vue重写了这个组件. npm.vue等等安装. 只是一个简单的日历组件,所以并不 ...

  4. vue初学实践之路——vue简单日历组件(3)

    这一篇我们来实现管理员修改每一天剩余数量的功能. <div id="calendar"> <div id="left"> <spa ...

  5. vue初学实践之路——vue简单日历组件(2)

    上一篇我们已经实现了基本的日历显示功能,这一次我们要加上预定的功能 废话不多说,上代码 <div id="calendar"> <!-- 年份 月份 --> ...

  6. vue自定义日历组件的实现

    实现一个日期组件,如图: components.js代码如下: Vue.component('sc-calendar',{ template:'<div class="scCalend ...

  7. vue 自定义日历组件

    <template> <div class=""> <div class="calendarTraffic" name=" ...

  8. 一个vue的日历组件

    说明: 1.基于element-ui开发的vue日历组件. 地址 更新: 1.增加value-format指定返回值的格式2.增加头部插槽自定义头部 <ele-calendar > < ...

  9. vue - 小日历项目制作中的问题与解决思路

    效果图: 项目难点: 1. 每个月的日期数是不定的,拢共需要几个格子? 按照教程的做法需要42个.所以遍历数字42,得到42个div做格子. 2. 格子的排版怎么做? 顶部的星期布局使用的flex水平 ...

随机推荐

  1. CSS & new class name

    CSS & new class name { test: /\.((s*)css|sass)$/, // test: /\.(css|scss|sass)$/, use: ExtractTex ...

  2. iframe & sandbox & 微前端

    iframe & sandbox & 微前端 沙箱,容器,隔离 sandbox demo svg progress bar https://stackoverflow.com/ques ...

  3. Electron All In One

    Electron All In One desktop app https://www.electronjs.org/docs/api/browser-window BrowserWindow 创建和 ...

  4. component & slot

    component & slot <template> <div class="myHeaderContainer"> <header cla ...

  5. JDK环境解析,安装和目的

    目录 1. JDK环境解析 1.1 JVM 1.2 JRE 1.3 JDK 2. JDK安装 2.1 为什么使用JDK8 2.1.1 更新 2.1.2 稳定 2.1.3 需求 2.2 安装JDK 2. ...

  6. 实现TensorRT-7.0插件自由!(如果不踩坑使用TensorRT插件功能)

    本系列为新TensorRT的第一篇,为什么叫新,因为之前已经写了两篇关于TensorRT的文章,是关于TensorRT-5.0版本的.好久没写关于TensorRT的文章了,所幸就以新来开头吧~ 接下来 ...

  7. Linux 网络分析必备技能:tcpdump 实战详解

    大家好,我是肖邦,这是我的第 11 篇原创文章. 今天要分享的是 tcpdump,它是 Linux 系统中特别有用的网络工具,通常用于故障诊断.网络分析,功能非常的强大. 相对于其它 Linux 工具 ...

  8. mybites框架遇到的坑之Mapper.xml文件不要随意加注释和ORA-00911

    原文链接:https://blog.csdn.net/streetlight8023/article/details/69388495/ 先说解决方法: org.mybatis.spring.MyBa ...

  9. C语言柔性数组和动态数组

    [前言]经常看到C语言里的两个数组,总结一下. 一.柔性数组 参考:https://www.cnblogs.com/veis/p/7073076.html #include<stdio.h> ...

  10. Reactive Spring实战 -- 理解Reactor的设计与实现

    Reactor是Spring提供的非阻塞式响应式编程框架,实现了Reactive Streams规范. 它提供了可组合的异步序列API,例如Flux(用于[N]个元素)和Mono(用于[0 | 1]个 ...