1.  

基于Vue的日历小功能,可根据实际开发情况按每年、每月、每周、进行切换

  1. <template>
  2. <div class="date">
  3. <!-- 年份 月份 -->
  4. <div class="month">
  5. <p>{{ currentYear }}年{{ currentMonth }}月</p>
  6. </div>
  7. <!-- 星期 -->
  8. <ul class="weekdays">
  9. <li></li>
  10. <li></li>
  11. <li></li>
  12. <li></li>
  13. <li></li>
  14. <li></li>
  15. <li></li>
  16. </ul>
  17. <!-- 日期 -->
  18. <ul class="days">
  19. <li @click="pick(day)" v-for="(day, index) in days" :key="index">
  20. <!--本月-->
  21. <span v-if="day.getMonth()+1 != currentMonth" class="other-month">{{ day.getDate() }}</span>
  22. <span v-else>
  23. <!--今天-->
  24. <span v-if="day.getFullYear() == new Date().getFullYear() && day.getMonth() == new Date().getMonth() && day.getDate() == new Date().getDate()" class="active">{{ day.getDate() }}</span>
  25. <span v-else>{{ day.getDate() }}</span>
  26. </span>
  27. </li>
  28. </ul>
  29. </div>
  30. </template>

js部分:目前默认显示一周,可根据实际情况更改

  1. <script>
  2.  
  3. export default {
  4. name: 'date',
  5.  
  6. data () {
  7. return {
  8. currentYear: 1970, // 年份
  9. currentMonth: 1, // 月份
  10. currentDay: 1, // 日期
  11. currentWeek: 1, // 星期
  12. days: [],
  13. }
  14. },
  15.  
  16. mounted () {
  17.  
  18. },
  19.  
  20. created () {
  21. this.initData(null)
  22. },
  23.  
  24. methods: {
  25. formatDate (year, month, day) {
  26. const y = year
  27. let m = month
  28. if (m < 10) m = `0${m}`
  29. let d = day
  30. if (d < 10) d = `0${d}`
  31. return `${y}-${m}-${d}`
  32. },
  33.  
  34. initData (cur) {
  35. let date = ''
  36. if (cur) {
  37. date = new Date(cur)
  38. } else {
  39. date = new Date()
  40. }
  41. this.currentDay = date.getDate() // 今日日期 几号
  42. this.currentYear = date.getFullYear() // 当前年份
  43. this.currentMonth = date.getMonth() + 1 // 当前月份
  44. this.currentWeek = date.getDay() // 1...6,0 // 星期几
  45. if (this.currentWeek === 0) {
  46. this.currentWeek = 7
  47. }
  48. const str = this.formatDate(this.currentYear, this.currentMonth, this.currentDay)// 今日日期 年-月-日
  49. this.days.length = 0
  50. // 今天是周日,放在第一行第7个位置,前面6个 这里默认显示一周,如果需要显示一个月,则第二个循环为 i<= 35- this.currentWeek
  51. /* eslint-disabled */
  52. for (let i = this.currentWeek - 1; i >= 0; i -= 1) {
  53. const d = new Date(str)
  54. d.setDate(d.getDate() - i)
  55. // console.log(y:" + d.getDate())
  56. this.days.push(d)
  57. }
  58. for (let i = 1; i <= 7 - this.currentWeek; i += 1) {
  59. const d = new Date(str)
  60. d.setDate(d.getDate() + i)
  61. this.days.push(d)
  62. }
  63. },
  64.  
  65. // 上个星期
  66. weekPre () {
  67. const d = this.days[0] // 如果当期日期是7号或者小于7号
  68. d.setDate(d.getDate() - 7)
  69. this.initData(d)
  70. },
  71.  
  72. // 下个星期
  73. weekNext () {
  74. const d = this.days[6] // 如果当期日期是7号或者小于7号
  75. d.setDate(d.getDate() + 7)
  76. this.initData(d)
  77. },
  78.  
  79. // 上一個月 传入当前年份和月份
  80. pickPre (year, month) {
  81. const d = new Date(this.formatDate(year, month, 1))
  82. d.setDate(0)
  83. this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1))
  84. },
  85.  
  86. // 下一個月 传入当前年份和月份
  87. pickNext (year, month) {
  88. const d = new Date(this.formatDate(year, month, 1))
  89. d.setDate(35)
  90. this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1))
  91. },
  92.  
  93. // 当前选择日期
  94. pick (date) {
  95. alert(this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate()))
  96. },
  97. },
  98. }
  99. </script>
  1. <style lang="scss">
  2. @import "~base";
  3.  
  4. .date {
  5. height: px2rem(180);
  6. color: #333;
  7.  
  8. .month {
  9. font-size: px2rem(24);
  10. text-align: center;
  11. margin-top: px2rem(20);
  12. }
  13.  
  14. .weekdays {
  15. display: flex;
  16. font-size: px2rem(28);
  17. margin-top: px2rem(20);
  18.  
  19. li {
  20. flex:;
  21. text-align: center;
  22. }
  23. }
  24.  
  25. .days {
  26. display: flex;
  27.  
  28. li {
  29. flex:;
  30. font-size: px2rem(30);
  31. text-align: center;
  32. margin-top: px2rem(10);
  33. line-height: px2rem(60);
  34.  
  35. .active {
  36. display: inline-block;
  37. width: px2rem(60);
  38. height: px2rem(60);
  39. color: #fff;
  40. border-radius: 50%;
  41. background-color: #fa6854;
  42. }
  43.  
  44. .other-month {
  45. color: #e4393c;
  46. }
  47. }
  48. }
  49. }
  50. </style>

相关参考链接;http://www.jb51.net/article/96402.htm

基于Vue的小日历(支持按周切换)的更多相关文章

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

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

  2. 一个基于vue的时钟

    前两天写了一个基于vue的小钟表,给大家分享一下. 其中时针和分针使用的是图片,结合transform制作:表盘刻度是通过transform和transformOrigin配合画的:外面的弧形框框,啊 ...

  3. vue-calendar 基于 vue 2.0 开发的轻量,高性能日历组件

    vue-calendar-component 基于 vue 2.0 开发的轻量,高性能日历组件 占用内存小,性能好,样式好看,可扩展性强 原生 js 开发,没引入第三方库 Why Github 上很多 ...

  4. 1个多商户、多平台版 微信小程序(多商户、多平台版),影城行业、影业连锁 多商户、多平台版微信小程序。(基于多平台版,支持在业务上 可给 每个单独影城 分发定制单独的小程序版本)

    1个 影城行业 微信小程序(多商户.多平台版), 影业连锁 多商户.多平台版微信小程序.(基于多平台版,支持在业务上 可给 每个单独影城 分发定制单独的小程序版本) 资讯QQ: 876635409  ...

  5. 基于Vue.js的uni-app前端框架结合.net core开发跨平台project

    一.由来 最近由于业务需要要开发一套公益的APP项目,因此结合所给出的需求最终采用uni-app这种跨平台前端框架以及.netcore快速搭建我们的项目,并且能做到一套代码跨多个平台. 当然在前期技术 ...

  6. 基于Vue.js的表格分页组件

    有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...

  7. 一次基于Vue.Js用户体验的优化

    .mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...

  8. 基于VUE框架 与 其他框架间的基本对比

    基于VUE框架的基本描述 与 其他框架间的基本对比 2018-11-03  11:01:14 A B React React 和 Vue 有许多相似之处,它们都有: 使用 Virtual DOM 提供 ...

  9. 一次基于Vue.Js的用户体验优化 (vue drag)

    一.写在前面 半年以前,第一次在项目上实践VueJs,由于在那之前,没有Angular,avalon等框架的实践经验,所以在Vue的使用上,没有给自己总结出更多的经验和体验.随着项目进行和优化改版,无 ...

随机推荐

  1. Jupyter Notebook使用小技巧

    在 C:\Windows\Fonts目录下找到Mircosoft YaHei UI字体,然后复制到[你的Python安装路径]/Lib/site-packages/matplotlib/mpl-dat ...

  2. WPF的消息机制(二)- WPF内部的5个窗口之隐藏消息窗口

    目录 WPF的消息机制(一)-让应用程序动起来 WPF的消息机制(二)-WPF内部的5个窗口 (1)隐藏消息窗口 (2)处理激活和关闭的消息的窗口和系统资源通知窗口 (3)用于用户交互的可见窗口 (4 ...

  3. HTML学习 框架

    iframe 在原来的页面嵌入其他页面 <iframe src="其他页面地址" width="宽" height="高" frame ...

  4. robotframework的学习笔记(十二)------DatabaseLibrary 库

    1.安装DatabaseLibrary库 DatabaseLibrary 下载地址:https://pypi.python.org/pypi/robotframework-databaselibrar ...

  5. 12.python进程\协程\异步IO

    进程 创建进程 from multiprocessing import Process import time def func(name): time.sleep(2) print('hello', ...

  6. js 数组API之filter的用法

    filter 查找数组中满足条件的元素,返回新数组:原数组不变 var subArr = arr.filter(function(value, index, array){ return 条件 }) ...

  7. IO多路复用

    1.事件驱动模型 上一篇写的协程仅仅是切换,本身不能实现并发,什么时候切换也不知道 那么什么时候切回去呢?怎么确定IO操作完了?通过回调函数  对于事件驱动型程序模型,它的流程大致如下: 开始---& ...

  8. 从Unity中的Attribute到AOP(三)

    上一篇我们对系统的Attributes进行了MSIL代码的查看,了解到了其本质就是一个类的构造函数.本章我们将编写自己的Attributes. 首先我们定义书的属性代码,如下: [AttributeU ...

  9. [wiki]CDN

    内容分发网 内容分发网络(Content delivery network或Content distribution network,缩写:CDN)是指一种通过互联网互相连接的电脑网络系统,利用最靠近 ...

  10. Nginx是如何处理Request的?

    nginx是如何匹配过来的请求,然后做处理的呢?这个匹配的过程可以分为两步: 1.选择server 2.选择location    选择server 仅仅匹配server name 加入Nginx的配 ...