vue实现日历
vue实现日历
之前在上家公司做过一个公司人员考勤的东西,里面需要用到日历,当时自己用vue随便写了一个,比较简单
下面代码是删掉了其他功能的代码,只留下日历部分
- <template>
- <div class="lookForMonth_wrapper">
- <div class="lookForMonth_top">
- <div class="selectDate">
- <div>{{year}} 年 {{month}} 月</div>
- <div class="upDownSelect">
- <div class="upDownSelect_item" @click="dateUp"></div>
- <div class="upDownSelect_item" @click="dateDown"></div>
- </div>
- </div>
- </div>
- <div class="calendar" :style="calendarStyle">
- <div v-for="(item,index) in calendarData" class="calendar_item" :key='index' :class="{ash:item.color==='ash',date:index>6&&item.color!=='ash'}">
- <p class="dateEdit">{{item.label}}<i class="el-icon-edit-outline" v-if="item.color!=='ash'&&index>=7"></i></p>
- <p v-if='index>6'>上班</p> // 打工人
- </div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "lookForMonth",
- data: () => {
- return {
- calendarData: [{label:"日"},{label: "一"}, {label:"二"},{label: "三"},{label: "四"},{label: "五"},{label: "六"}], //日历循环渲染数据
- year: 0, //当前日期年
- month: 0, //当前日期月数
- date: 0, //当前日期号数
- day: -1, //当前星期几
- };
- },
- filters:{
- },
- computed: {
- // 根据当月日期详情更改日历表格高度
- calendarStyle() {
- if (this.calendarData.length > 42) {
- return "height: 701px;";
- } else {
- return "height: 601px;";
- }
- }
- },
- async created(){
- // 获取当前日期数据
- this.getNow();
- // 获取当前月份一号的时间戳
- let firstTime = +new Date(this.year,this.month-1,1,0,0,0)
- this.getCalendarDate(); // 给calendarData添加当月数据
- },
- mounted() {
- },
- methods: {
- // 获取当前时间
- getNow() {
- let now = new Date()
- this.year = +now.getFullYear()
- this.month = +now.getMonth() + 1
- this.date = +now.getDate()
- this.day = +now.getDay()
- },
- // 获取每个月的天数
- monthDay(month) {
- if ([1,3,5,7,8,10,12].includes(month)) {
- return 31
- } else if ([4,6,9,11].includes(month)) {
- return 30
- } else if (month === 2) {
- // 判断当年是否为闰年
- if (
- (this.year % 4 === 0 && this.year % 100 !== 0) ||
- this.year % 400 === 0
- ) {
- return 29
- } else {
- return 28
- }
- }
- },
- // 给calendarData添加当月数据
- getCalendarDate() {
- // 获取当前月份一号星期几
- let firstDay = new Date(this.year + "-" + this.month + "-" + "01").getDay();
- this.calendarData = [{label:"日"},{label: "一"}, {label:"二"},{label: "三"},{label: "四"},{label: "五"},{label: "六"}];
- let num = parseInt(firstDay);
- let nowDays = this.monthDay(this.month);
- let lastMonth = this.month - 1>0?this.month - 1:12;
- let lastDays = this.monthDay(lastMonth);
- // 循环添加上一个月数据
- for (let i = 0; i < num; i++) {
- this.calendarData.push({label:lastDays - num + i + 1,color:'ash'});
- }
- // 循环添加当月数据
- for (let i = 0; i < nowDays; i++) {
- this.calendarData.push({label:i + 1});
- }
- // 循环添加下一个月数据
- if (this.calendarData.length % 7 !== 0) {
- let surplusDay = 7 - (this.calendarData.length % 7);
- for (let i = 0; i < surplusDay; i++) {
- this.calendarData.push({label:i + 1,color:'ash'});
- }
- }
- this.loading = false
- },
- // 将日期调上
- dateUp() {
- this.month--;
- if (this.month <= 0) {
- this.year--;
- this.month = 12;
- }
- this.getCalendarDate(); // 给calendarData添加当月数据
- },
- // 将日期调下
- dateDown() {
- this.month++;
- if (this.month > 12) {
- this.year++;
- this.month = 1;
- }
- this.getCalendarDate(); // 给calendarData添加当月数据
- },
- }
- };
- </script>
- <style lang="scss" scoped>
- .lookForMonth_wrapper {
- padding: 20px;
- width: 701px;
- margin: auto;
- }
- .lookForMonth_top {
- margin-bottom: 20px;
- overflow: hidden;
- .selectTeacher {
- float: left;
- }
- .selectDate {
- height: 30px;
- line-height: 30px;
- float: right;
- display: flex;
- .upDownSelect {
- display: flex;
- flex-direction: column;
- margin-top: -2px;
- margin-left: 5px;
- .upDownSelect_item {
- width: 0;
- height: 0;
- border: 7px solid transparent;
- cursor: pointer;
- }
- .upDownSelect_item:nth-child(1) {
- border-bottom: 7px solid #666;
- margin-bottom: 5px;
- &:hover {
- border-bottom: 7px solid skyblue;
- }
- }
- .upDownSelect_item:nth-child(2) {
- border-top: 7px solid #666;
- &:hover {
- border-top: 7px solid skyblue;
- }
- }
- }
- }
- }
- /* 日历表样式=======================================↓ */
- .calendar {
- width: 701px;
- border-top: 1px solid #ccc;
- border-left: 1px solid #ccc;
- display: flex;
- flex-wrap: wrap;
- box-sizing: border-box;
- .calendar_item {
- box-sizing: border-box;
- width: 100px;
- height: 100px;
- border-right: 1px solid #ccc;
- border-bottom: 1px solid #ccc;
- text-align: center;
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- &.date:hover{
- background: #eee;
- }
- .status{
- margin-top: 10px;
- &.textBlue{
- color: blue;
- }
- &.textRed{
- color: brown;
- }
- }
- .el-icon-edit-outline{
- cursor: pointer;
- margin-left: 7px;
- }
- }
- .ash{
- color: gainsboro;
- }
- .dateEdit{
- margin-bottom: 10px;
- }
- }
- </style>
效果如下:
vue实现日历的更多相关文章
- Vue自定义日历组件
今天给大家介绍Vue的日历组件,可自定义样式.日历类型及支持扩展,可自定义事件回调.Props数据传输. 线上demo效果 示例 Template: <Calendar :sundayStart ...
- 使用ant design vue的日历组件,实现一个简单交易日与非交易日的切换
使用ant design vue的日历组件,实现一个简单交易日与非交易日的切换 需求: 日历区分交易日.非交易日 可以切换面板查看整年交易日信息 可以在手动调整交易日.非交易日 演示实例 序--使用软 ...
- vue初学实践之路——vue简单日历组件(1)
---恢复内容开始--- 最近做的项目有一个需求,需要有一个日历组件供预定功能使用,之前的代码过于繁琐复杂,所以我采用vue重写了这个组件. npm.vue等等安装. 只是一个简单的日历组件,所以并不 ...
- vue初学实践之路——vue简单日历组件(3)
这一篇我们来实现管理员修改每一天剩余数量的功能. <div id="calendar"> <div id="left"> <spa ...
- vue初学实践之路——vue简单日历组件(2)
上一篇我们已经实现了基本的日历显示功能,这一次我们要加上预定的功能 废话不多说,上代码 <div id="calendar"> <!-- 年份 月份 --> ...
- vue自定义日历组件的实现
实现一个日期组件,如图: components.js代码如下: Vue.component('sc-calendar',{ template:'<div class="scCalend ...
- vue 自定义日历组件
<template> <div class=""> <div class="calendarTraffic" name=" ...
- 一个vue的日历组件
说明: 1.基于element-ui开发的vue日历组件. 地址 更新: 1.增加value-format指定返回值的格式2.增加头部插槽自定义头部 <ele-calendar > < ...
- vue - 小日历项目制作中的问题与解决思路
效果图: 项目难点: 1. 每个月的日期数是不定的,拢共需要几个格子? 按照教程的做法需要42个.所以遍历数字42,得到42个div做格子. 2. 格子的排版怎么做? 顶部的星期布局使用的flex水平 ...
随机推荐
- CSS & new class name
CSS & new class name { test: /\.((s*)css|sass)$/, // test: /\.(css|scss|sass)$/, use: ExtractTex ...
- iframe & sandbox & 微前端
iframe & sandbox & 微前端 沙箱,容器,隔离 sandbox demo svg progress bar https://stackoverflow.com/ques ...
- Electron All In One
Electron All In One desktop app https://www.electronjs.org/docs/api/browser-window BrowserWindow 创建和 ...
- component & slot
component & slot <template> <div class="myHeaderContainer"> <header cla ...
- 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. ...
- 实现TensorRT-7.0插件自由!(如果不踩坑使用TensorRT插件功能)
本系列为新TensorRT的第一篇,为什么叫新,因为之前已经写了两篇关于TensorRT的文章,是关于TensorRT-5.0版本的.好久没写关于TensorRT的文章了,所幸就以新来开头吧~ 接下来 ...
- Linux 网络分析必备技能:tcpdump 实战详解
大家好,我是肖邦,这是我的第 11 篇原创文章. 今天要分享的是 tcpdump,它是 Linux 系统中特别有用的网络工具,通常用于故障诊断.网络分析,功能非常的强大. 相对于其它 Linux 工具 ...
- mybites框架遇到的坑之Mapper.xml文件不要随意加注释和ORA-00911
原文链接:https://blog.csdn.net/streetlight8023/article/details/69388495/ 先说解决方法: org.mybatis.spring.MyBa ...
- C语言柔性数组和动态数组
[前言]经常看到C语言里的两个数组,总结一下. 一.柔性数组 参考:https://www.cnblogs.com/veis/p/7073076.html #include<stdio.h> ...
- Reactive Spring实战 -- 理解Reactor的设计与实现
Reactor是Spring提供的非阻塞式响应式编程框架,实现了Reactive Streams规范. 它提供了可组合的异步序列API,例如Flux(用于[N]个元素)和Mono(用于[0 | 1]个 ...