基于Vue的小日历(支持按周切换)
基于Vue的日历小功能,可根据实际开发情况按每年、每月、每周、进行切换
- <template>
- <div class="date">
- <!-- 年份 月份 -->
- <div class="month">
- <p>{{ currentYear }}年{{ currentMonth }}月</p>
- </div>
- <!-- 星期 -->
- <ul class="weekdays">
- <li>一</li>
- <li>二</li>
- <li>三</li>
- <li>四</li>
- <li>五</li>
- <li>六</li>
- <li>日</li>
- </ul>
- <!-- 日期 -->
- <ul class="days">
- <li @click="pick(day)" v-for="(day, index) in days" :key="index">
- <!--本月-->
- <span v-if="day.getMonth()+1 != currentMonth" class="other-month">{{ day.getDate() }}</span>
- <span v-else>
- <!--今天-->
- <span v-if="day.getFullYear() == new Date().getFullYear() && day.getMonth() == new Date().getMonth() && day.getDate() == new Date().getDate()" class="active">{{ day.getDate() }}</span>
- <span v-else>{{ day.getDate() }}</span>
- </span>
- </li>
- </ul>
- </div>
- </template>
js部分:目前默认显示一周,可根据实际情况更改
- <script>
- export default {
- name: 'date',
- data () {
- return {
- currentYear: 1970, // 年份
- currentMonth: 1, // 月份
- currentDay: 1, // 日期
- currentWeek: 1, // 星期
- days: [],
- }
- },
- mounted () {
- },
- created () {
- this.initData(null)
- },
- methods: {
- formatDate (year, month, day) {
- const y = year
- let m = month
- if (m < 10) m = `0${m}`
- let d = day
- if (d < 10) d = `0${d}`
- return `${y}-${m}-${d}`
- },
- initData (cur) {
- let date = ''
- if (cur) {
- date = new Date(cur)
- } else {
- date = new Date()
- }
- this.currentDay = date.getDate() // 今日日期 几号
- this.currentYear = date.getFullYear() // 当前年份
- this.currentMonth = date.getMonth() + 1 // 当前月份
- this.currentWeek = date.getDay() // 1...6,0 // 星期几
- if (this.currentWeek === 0) {
- this.currentWeek = 7
- }
- const str = this.formatDate(this.currentYear, this.currentMonth, this.currentDay)// 今日日期 年-月-日
- this.days.length = 0
- // 今天是周日,放在第一行第7个位置,前面6个 这里默认显示一周,如果需要显示一个月,则第二个循环为 i<= 35- this.currentWeek
- /* eslint-disabled */
- for (let i = this.currentWeek - 1; i >= 0; i -= 1) {
- const d = new Date(str)
- d.setDate(d.getDate() - i)
- // console.log(y:" + d.getDate())
- this.days.push(d)
- }
- for (let i = 1; i <= 7 - this.currentWeek; i += 1) {
- const d = new Date(str)
- d.setDate(d.getDate() + i)
- this.days.push(d)
- }
- },
- // 上个星期
- weekPre () {
- const d = this.days[0] // 如果当期日期是7号或者小于7号
- d.setDate(d.getDate() - 7)
- this.initData(d)
- },
- // 下个星期
- weekNext () {
- const d = this.days[6] // 如果当期日期是7号或者小于7号
- d.setDate(d.getDate() + 7)
- this.initData(d)
- },
- // 上一個月 传入当前年份和月份
- pickPre (year, month) {
- const d = new Date(this.formatDate(year, month, 1))
- d.setDate(0)
- this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1))
- },
- // 下一個月 传入当前年份和月份
- pickNext (year, month) {
- const d = new Date(this.formatDate(year, month, 1))
- d.setDate(35)
- this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1))
- },
- // 当前选择日期
- pick (date) {
- alert(this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate()))
- },
- },
- }
- </script>
- <style lang="scss">
- @import "~base";
- .date {
- height: px2rem(180);
- color: #333;
- .month {
- font-size: px2rem(24);
- text-align: center;
- margin-top: px2rem(20);
- }
- .weekdays {
- display: flex;
- font-size: px2rem(28);
- margin-top: px2rem(20);
- li {
- flex:;
- text-align: center;
- }
- }
- .days {
- display: flex;
- li {
- flex:;
- font-size: px2rem(30);
- text-align: center;
- margin-top: px2rem(10);
- line-height: px2rem(60);
- .active {
- display: inline-block;
- width: px2rem(60);
- height: px2rem(60);
- color: #fff;
- border-radius: 50%;
- background-color: #fa6854;
- }
- .other-month {
- color: #e4393c;
- }
- }
- }
- }
- </style>
相关参考链接;http://www.jb51.net/article/96402.htm
基于Vue的小日历(支持按周切换)的更多相关文章
- 基于Vue的简单日历组件
日历组件 由于移动端项目中需要用到日历组件,网上找了下,没看到几个合适的,就尝试着自己写一个.然后发现也不是很复杂,目前只做了最基本的功能,大家也可以拿去做做二次开发. 如何写一个日历组件 基础效果如 ...
- 一个基于vue的时钟
前两天写了一个基于vue的小钟表,给大家分享一下. 其中时针和分针使用的是图片,结合transform制作:表盘刻度是通过transform和transformOrigin配合画的:外面的弧形框框,啊 ...
- vue-calendar 基于 vue 2.0 开发的轻量,高性能日历组件
vue-calendar-component 基于 vue 2.0 开发的轻量,高性能日历组件 占用内存小,性能好,样式好看,可扩展性强 原生 js 开发,没引入第三方库 Why Github 上很多 ...
- 1个多商户、多平台版 微信小程序(多商户、多平台版),影城行业、影业连锁 多商户、多平台版微信小程序。(基于多平台版,支持在业务上 可给 每个单独影城 分发定制单独的小程序版本)
1个 影城行业 微信小程序(多商户.多平台版), 影业连锁 多商户.多平台版微信小程序.(基于多平台版,支持在业务上 可给 每个单独影城 分发定制单独的小程序版本) 资讯QQ: 876635409 ...
- 基于Vue.js的uni-app前端框架结合.net core开发跨平台project
一.由来 最近由于业务需要要开发一套公益的APP项目,因此结合所给出的需求最终采用uni-app这种跨平台前端框架以及.netcore快速搭建我们的项目,并且能做到一套代码跨多个平台. 当然在前期技术 ...
- 基于Vue.js的表格分页组件
有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...
- 一次基于Vue.Js用户体验的优化
.mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...
- 基于VUE框架 与 其他框架间的基本对比
基于VUE框架的基本描述 与 其他框架间的基本对比 2018-11-03 11:01:14 A B React React 和 Vue 有许多相似之处,它们都有: 使用 Virtual DOM 提供 ...
- 一次基于Vue.Js的用户体验优化 (vue drag)
一.写在前面 半年以前,第一次在项目上实践VueJs,由于在那之前,没有Angular,avalon等框架的实践经验,所以在Vue的使用上,没有给自己总结出更多的经验和体验.随着项目进行和优化改版,无 ...
随机推荐
- Jupyter Notebook使用小技巧
在 C:\Windows\Fonts目录下找到Mircosoft YaHei UI字体,然后复制到[你的Python安装路径]/Lib/site-packages/matplotlib/mpl-dat ...
- WPF的消息机制(二)- WPF内部的5个窗口之隐藏消息窗口
目录 WPF的消息机制(一)-让应用程序动起来 WPF的消息机制(二)-WPF内部的5个窗口 (1)隐藏消息窗口 (2)处理激活和关闭的消息的窗口和系统资源通知窗口 (3)用于用户交互的可见窗口 (4 ...
- HTML学习 框架
iframe 在原来的页面嵌入其他页面 <iframe src="其他页面地址" width="宽" height="高" frame ...
- robotframework的学习笔记(十二)------DatabaseLibrary 库
1.安装DatabaseLibrary库 DatabaseLibrary 下载地址:https://pypi.python.org/pypi/robotframework-databaselibrar ...
- 12.python进程\协程\异步IO
进程 创建进程 from multiprocessing import Process import time def func(name): time.sleep(2) print('hello', ...
- js 数组API之filter的用法
filter 查找数组中满足条件的元素,返回新数组:原数组不变 var subArr = arr.filter(function(value, index, array){ return 条件 }) ...
- IO多路复用
1.事件驱动模型 上一篇写的协程仅仅是切换,本身不能实现并发,什么时候切换也不知道 那么什么时候切回去呢?怎么确定IO操作完了?通过回调函数 对于事件驱动型程序模型,它的流程大致如下: 开始---& ...
- 从Unity中的Attribute到AOP(三)
上一篇我们对系统的Attributes进行了MSIL代码的查看,了解到了其本质就是一个类的构造函数.本章我们将编写自己的Attributes. 首先我们定义书的属性代码,如下: [AttributeU ...
- [wiki]CDN
内容分发网 内容分发网络(Content delivery network或Content distribution network,缩写:CDN)是指一种通过互联网互相连接的电脑网络系统,利用最靠近 ...
- Nginx是如何处理Request的?
nginx是如何匹配过来的请求,然后做处理的呢?这个匹配的过程可以分为两步: 1.选择server 2.选择location 选择server 仅仅匹配server name 加入Nginx的配 ...