经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue实现日历组件
来源:jb51  时间:2022/4/18 12:05:15  对本文有异议

基于VUE实现日历组件,供大家参考,具体内容如下

年和月份是使用输入框来切换的,没有做成选择框,??和??切换月份,红色选取是选取的日期
实现思路和网上的大多数一样,首先是把月份的天数存进一个数组,

  1. monthDay:[31,'',31,30,31,30,31,31,30,31,30,31],

由于二月的天数是不确定的,所以就先设置为空

然后去求选择的月份的第一天是星期几,通过 Date.getDay()函数,这个函数有一个注意事项,就是如果是星期天,他会返回0,这需要我们自己去处理一下

图中,2019年1月1号是星期二,所以前面就要给它一个空格块
下面代码中第一层循环就是在循环空格块,spaceDay表示需要几个空格

  1. <p v-for="item in spaceDay" :key="item.id"></p>
  2. <p v-for="(item,idx) in (monthDay[this.month-1] || 30)" @click="setDay(idx)" :class="idx==activeDay?'active':''" :key="item.id">{{item}}</p>

下面贴出完整代码

  1. <template>
  2. ? ? <div id="calender">
  3. ? ? ? ? <div class="txt-c p-10">
  4. ? ? ? ? ? ? <span @click="prev"> ?? </span>
  5. ? ? ? ? ? ? <input type="text" v-model="year">
  6. ? ? ? ? ? ? <input type="text" v-model="month" class="month">
  7. ? ? ? ? ? ? <span @click="next"> ????? </span>
  8. ? ? ? ? </div>
  9. ? ? ? ? <div class="weekDay flex jc-sb p-5 day" >
  10. ? ? ? ? ? ? <p v-for="item in weekList" :key="item.id">{{item}}</p>
  11. ? ? ? ? </div>
  12. ? ? ? ? <div class="weekDay flex p-5 day">
  13. ? ? ? ? ? ? <p v-for="item in spaceDay" :key="item.id"></p>
  14. ? ? ? ? ? ? <p v-for="(item,idx) in (monthDay[this.month-1] || 30)" @click="setDay(idx)" :class="idx==activeDay?'active':''" :key="item.id">{{item}}</p>
  15. ? ? ? ? </div>
  16. ? ? </div>
  17. </template>
  18.  
  19. <script>
  20. ? ? export default {
  21. ? ? ? ? name: "calender",
  22. ? ? ? ? data(){
  23. ? ? ? ? ? ? return{
  24. ? ? ? ? ? ? ? ? year: '', // 年份
  25. ? ? ? ? ? ? ? ? month: '', // 月份
  26. ? ? ? ? ? ? ? ? day: '', // 天数
  27. ? ? ? ? ? ? ? ? current: '', // 当前时间
  28. ? ? ? ? ? ? ? ? weekList:['周一','周二','周三','周四','周五','周六','周日'],
  29. ? ? ? ? ? ? ? ? monthDay:[31,'',31,30,31,30,31,31,30,31,30,31],
  30. ? ? ? ? ? ? ? ? activeDay: '', // 选中的日期
  31. ? ? ? ? ? ? ? ? spaceDay: '', // 每个月第一天是星期几
  32. ? ? ? ? ? ? ? ? February:'' // 判断2月份的天数
  33. ? ? ? ? ? ? }
  34. ? ? ? ? },
  35. ? ? ? ? created(){
  36. ? ? ? ? ? ? this.current = new Date()
  37. ? ? ? ? ? ? this.getTheCurrentDate()
  38. ? ? ? ? ? ? this.getMonthFisrtDay()
  39. ? ? ? ? ? ? this.February = this.isLeapYear(this.year) ? 28 : 29
  40. ? ? ? ? ? ? this.monthDay.splice(1,1,this.February)
  41. ? ? ? ? },
  42. ? ? ? ? watch:{
  43. ? ? ? ? ? month(){
  44. ? ? ? ? ? ? ? if(this.month>12||this.month<1){
  45. ? ? ? ? ? ? ? ? ? console.log('请输入正确月份')
  46. ? ? ? ? ? ? ? ? ? return
  47. ? ? ? ? ? ? ? }
  48. ? ? ? ? ? ? ? this.getMonthFisrtDay()
  49. ? ? ? ? ? },
  50. ? ? ? ? ? year(){
  51. ? ? ? ? ? ? ? this.getMonthFisrtDay()
  52. ? ? ? ? ? }
  53. ? ? ? ? },
  54. ? ? ? ? methods:{
  55. ? ? ? ? ? ? // 判断是否是闰年
  56. ? ? ? ? ? ? isLeapYear(year){
  57. ? ? ? ? ? ??
  58. ? ? ? ? ? ? ? ? return year % 4 == 0 && year % 100 != 0 ||year % 400 == 0;
  59. ? ? ? ? ? ? },
  60. ? ? ? ? ? ? // 选取特定天数
  61. ? ? ? ? ? ? setDay(idx){
  62. ? ? ? ? ? ? ? ? this.activeDay = idx
  63. ? ? ? ? ? ? ? ? this.day = idx + 1
  64. ? ? ? ? ? ? ? ? console.log('选择的日期是'+this.year+' '+this.month+' '+this.day)
  65. ? ? ? ? ? ? },
  66. ? ? ? ? ? ? // 判断月份的第一天是星期几
  67. ? ? ? ? ? ? getMonthFisrtDay(){
  68. ? ? ? ? ? ? ? ? var firstDayOfCurrentMonth = new Date(this.year,this.month-1,1) // 某年某月的第一天
  69. ? ? ? ? ? ? ? ? if(firstDayOfCurrentMonth.getDay() == 0){
  70. ? ? ? ? ? ? ? ? ? ? this.spaceDay = 6
  71. ? ? ? ? ? ? ? ? } else {
  72. ? ? ? ? ? ? ? ? ? ? this.spaceDay = firstDayOfCurrentMonth.getDay() - 1
  73. ? ? ? ? ? ? ? ? }
  74. ? ? ? ? ? ? },
  75. ? ? ? ? ? ? // 获取当前的日期
  76. ? ? ? ? ? ? getTheCurrentDate(){
  77. ? ? ? ? ? ? ? ? this.year = this.current.getFullYear()
  78. ? ? ? ? ? ? ? ? this.month = this.current.getMonth() + 1
  79. ? ? ? ? ? ? ? ? this.day = this.current.getDate()
  80. ? ? ? ? ? ? },
  81. ? ? ? ? ? ? prev(){
  82. ? ? ? ? ? ? ? ? if(this.month==1){
  83. ? ? ? ? ? ? ? ? ? ? this.year--
  84. ? ? ? ? ? ? ? ? ? ? this.month=12
  85. ? ? ? ? ? ? ? ? }else{
  86. ? ? ? ? ? ? ? ? ? ? this.month--
  87. ? ? ? ? ? ? ? ? }
  88. ? ? ? ? ? ? ? ? this.activeDay = 0
  89. ? ? ? ? ? ? ? ? this.getMonthFisrtDay()
  90. ? ? ? ? ? ? },
  91. ? ? ? ? ? ? next(){
  92. ? ? ? ? ? ? ? ? if(this.month==12){
  93. ? ? ? ? ? ? ? ? ? ? this.year++
  94. ? ? ? ? ? ? ? ? ? ? this.month=1
  95. ? ? ? ? ? ? ? ? }else{
  96. ? ? ? ? ? ? ? ? ? ? this.month++
  97. ? ? ? ? ? ? ? ? }
  98. ? ? ? ? ? ? ? ? this.activeDay = 0
  99. ? ? ? ? ? ? ? ? this.getMonthFisrtDay()
  100. ? ? ? ? ? ? }
  101. ? ? ? ? }
  102. ? ? }
  103. </script>
  104.  
  105. <style lang="scss" scoped>
  106. #calender{
  107. ? ? .txt-c{
  108. ? ? ? ? text-align: center;
  109. ? ? }
  110. ? ? .p-10{
  111. ? ? ? ? padding: 2rem;
  112. ? ? }
  113. ? ? .p-5{
  114. ? ? ? ? padding: 1rem;
  115. ? ? }
  116. ? ? .flex {
  117. ? ? ? ? display: flex;
  118. ? ? }
  119. ? ? .jc-sb{
  120. ? ? ? ? justify-content: space-between;
  121. ? ? }
  122. ? ? input{
  123. ? ? ? ? width: 50px;
  124. ? ? ? ? &.month{
  125. ? ? ? ? ? ? width: 30px;
  126. ? ? ? ? }
  127. ? ? }
  128. ? ? .day{
  129. ? ? ? ? flex-wrap: wrap;
  130. ? ? ? ? p{
  131. ? ? ? ? ? ? width: 14.28%;
  132. ? ? ? ? ? ? /*flex: 0 0 0 ;*/
  133. ? ? ? ? ? ? text-align: center;
  134. ? ? ? ? ? ? line-height: 2.4rem;
  135. ? ? ? ? ? ? height: 2.4rem;
  136. ? ? ? ? ? ? position: relative;
  137. ? ? ? ? ? ? z-index: 100;
  138. ? ? ? ? ? ? &.active{
  139. ? ? ? ? ? ? ? ? color: #fff;
  140. ? ? ? ? ? ? }
  141. ? ? ? ? ? ? &.active:before{
  142. ? ? ? ? ? ? ? ? content: '';
  143. ? ? ? ? ? ? ? ? height: 2.5rem;
  144. ? ? ? ? ? ? ? ? width: 2.5rem;
  145. ? ? ? ? ? ? ? ? position: absolute;
  146. ? ? ? ? ? ? ? ? z-index: -1;
  147. ? ? ? ? ? ? ? ? left: 0;
  148. ? ? ? ? ? ? ? ? top: 0;
  149. ? ? ? ? ? ? ? ? transform: translateX(50%);
  150. ? ? ? ? ? ? ? ? border-radius: 50%;
  151. ? ? ? ? ? ? ? ? background: #e97163;
  152. ? ? ? ? ? ? ? ? color: #fff;
  153. ? ? ? ? ? ? }
  154. ? ? ? ? }
  155. ? ? }
  156. }
  157. </style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号