自适配日历组件开发

简介: 自适配日历组件开发

自适配日历组件开发

效果图

PC端

移动端

预览

预览地址:预览地址

1、传入参数

1.1、顶部背景图片

如上图红圈区域的照片背景设置

在组件参数中定义

bgSrc: {
    type: String,
        default: 'https://images8.alphacoders.com/992/992329.jpg'
}
1.2、日历标题

如上图圈住区域文字设置

在组件参数中定义

title: {
    type: String,
    default: '日历'
}

2、回调方法

2.1、选中日期

使用this.$emit()向父组件传递数据。

在组件日期点击事件中执行。

clickDay (day) {
    this.selectDay = day
    this.$emit('selectDay', day)
}
2.2、切换月份

使用this.$emit()向父组件传递数据。

在组件日期点击事件中执行。

//上个月
toPreMonth () {
    let year = this.selectMonth.split('-')[0]
    let month = this.selectMonth.split('-')[1]
    month = parseInt(month) - 1
    if (month === 0) {
        month = 12
        year = parseInt(year) - 1
    }
    this.days = this.fillDays(year, month)
    this.$emit('changeMonth', year + '-' + this.zero(month))
},
//下个月
toNextMonth () {
    let year = this.selectMonth.split('-')[0]
    let month = this.selectMonth.split('-')[1]
    month = parseInt(month) + 1
    if (month === 13) {
        month = 1
        year = parseInt(year) + 1
    }
    this.days = this.fillDays(year, month)
    this.$emit('changeMonth', year + '-' + this.zero(month))
}

3、组件js模块开发流程

3.1、月份天数确认
3.1.1、判断润年
/**
 * 判断润年
 * @param {string} year 需要判断的年份
 * @return {Boolean}
 */
function isLeap(year) {
    if((year%4==0 && year%100!=0)||(year%400==0)){
        return true;
    }
    return false;
}
3.1.2、获取月份天数
/**
 * 获取月份天数
 * @param {string} year  年份
 * @param {string} month 月份
 * @return {string}
 */
function getMonthDays(year,month) {
    month = parseInt(month) - 1;
    if(month < 0 || month > 11) return ''
    let months = [31,28,31,30,31,30,31,31,30,31,30,31];
    if(isLeap(year)){
        months[1] = 29;
    }
    return months[month];
}
3.1.3、获取星期
/**
 * 获取星期
 * @param {string} date 需要获取星期的日期
 * @return {string}
 */
function getWeek(date){
    let weeks = new Array("日","一","二","三","四","五","六");
    let Stamp = new Date(date);
    console.log(weeks[Stamp.getDay()])
}
3.1.4、补充满天数
/**
 * 补零
 * @param {string} str 需要补零的数
 * @return {string}
 */
function zero(str){
    return str > 9 ? str : '0' + str;
}
/**
 * 补充满天数
 * @param {string} year  年份
 * @param {string} month 月份
 * @return {string}
 */
function fillDays(year,month) {
    const months = getMonthDays(year,month);
    const startWeek = getWeek(year + '-' + month + '-' + '01');
    const endWeek = getWeek(year + '-' + month + '-' + months);
    year = parseInt(year);
    month = parseInt(month);
    let preYear = year;
    let preMonth = month - 1;
    if(preMonth == 0){
        preMonth = 12;
        preYear = year - 1;
    }
    const preMonths = getMonthDays(preYear,preMonth);
    let nextYear = year;
    let nextMonth = month + 1;
    if(nextMonth == 13){
        nextMonth = 1;
        nextYear = year + 1;
    }
    const nextMonths = getMonthDays(nextYear,nextMonth);
    let days = [];
    for(let i = 0; i < startWeek; i++){
        days.unshift(preYear + '-' + preMonth + '-' + (preMonths - i));
    }
    for(let i = 1; i <= months; i++){
        days.push(year + '-' + zero(month) + '-' + zero(i));
    }
    for(let i = 0; i < (6 - endWeek); i++){
        days.push(nextYear + '-' + nextMonth + '-0' + (i + 1));
    }
    return days;
}
3.2、点击事件
3.2.1、月份切换
toPreMonth () {
    let year = this.selectMonth.split('-')[0]
    let month = this.selectMonth.split('-')[1]
    month = parseInt(month) - 1
    if (month === 0) {
        month = 12
        year = parseInt(year) - 1
    }
    this.days = this.fillDays(year, month)
    this.$emit('changeMonth', year + '-' + this.zero(month))
},
toNextMonth () {
    let year = this.selectMonth.split('-')[0]
    let month = this.selectMonth.split('-')[1]
    month = parseInt(month) + 1
    if (month === 13) {
        month = 1
        year = parseInt(year) + 1
    }
    this.days = this.fillDays(year, month)
    this.$emit('changeMonth', year + '-' + this.zero(month))
}
3.2.2、日期点击
clickDay (day) {
    this.selectDay = day
    this.$emit('selectDay', day)
}

4、html模块

<template>
  <div>
    <div id="header" class="header">
      <div class="header-title">{{title}}</div>
      <div class="btn-list">
        <div class="btn-list-left">
          <div class="btn-pre" @click="toPreMonth()"><</div>
          <div class="select-month">{{selectMonth}}</div>
          <div class="btn-next" @click="toNextMonth()">></div>
        </div>
        <div class="btn-today" @click="toNowDay()">回到今天</div>
      </div>
    </div>
    <div class="content" id="content">
      <div class="calendar-content">
       <div class="grid-week grid" v-for="(item,index) in weeks" :key="index">
         周{{item}}
       </div>
       <div @click="clickDay(item)"
          class="grid-day grid"
          :class="{'selected': item == selectDay}"
          v-for="(item,index) in days"
          :key="index">
         {{item.split('-')[2]}}
       </div>
      </div>
    </div>
  </div>
</template>

5、CSS样式

<style lang="scss" scoped>
  @media screen and (max-width:500px) {
    .header{
      height: calc(100vw * 9 / 16);
    }
  }
  .header{
    display: flex;
    flex-direction: column;
    .header-title{
      line-height: 5rem;
    }
    .btn-list{
      display: flex;
      padding: 1rem;
      margin-top: auto;
      .btn-list-left{
        padding: 0.5rem;
        width: 40%;
        display: flex;
        .select-month{
          flex: 2;
        }
        .btn-pre{
          flex: 1;
          background-color: #0080FF;
        }
        .btn-next{
          flex: 1;
          background-color: #0080FF;
        }
      }
      .btn-today{
        padding: 0.5rem;
        margin-left: auto;
        margin-right: 1rem;
        background-color: #076678;
        color: white;
      }
    }
  }
  .calendar-content{
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    .selected{
      background-color: #007FAA;
    }
    .grid{
      width: calc((100% - 9px)/7);
      height: 3rem;
      line-height: 3rem;
      border-left: #005599 solid 1px;
      border-bottom: #005599 solid 1px;
    }
    .grid-week{
      border-top: #005599 solid 1px;
    }
    .grid-week:nth-child(7){
      border-right: #005599 solid 1px;
    }
    .grid-day:nth-child(14){
      border-right: #005599 solid 1px;
    }
    .grid-day:nth-child(21){
      border-right: #005599 solid 1px;
    }
    .grid-day:nth-child(28){
      border-right: #005599 solid 1px;
    }
    .grid-day:nth-child(35){
      border-right: #005599 solid 1px;
    }
    .grid-day:nth-child(42){
      border-right: #005599 solid 1px;
    }
  }
</style>

源码地址

Gitee:https://gitee.com/zheng_yongtao/jyeontu-component-warehouse/tree/master/components

喜欢的可以给个start★★★★★

目录
相关文章
|
安全 Linux Nacos
解决“nacos默认secret.key配置不当权限绕过漏洞“
解决“nacos默认secret.key配置不当权限绕过漏洞“
3594 0
|
SQL 存储 分布式计算
Hive数据仓库设计与优化策略:面试经验与必备知识点解析
本文深入探讨了Hive数据仓库设计原则(分区、分桶、存储格式选择)与优化策略(SQL优化、内置优化器、统计信息、配置参数调整),并分享了面试经验及常见问题,如Hive与RDBMS的区别、实际项目应用和与其他组件的集成。通过代码样例,帮助读者掌握Hive核心技术,为面试做好充分准备。
1868 0
|
云安全 SQL 弹性计算
阿里云提示网站后门发现后门(Webshell)文件的解决办法
2018年10月27日接到新客户网站服务器被上传了webshell脚本木马后门问题的求助,对此我们sine安全公司针对此阿里云提示的安全问题进行了详细分析,ECS服务器被阿里云提示异常网络连接-可疑WebShell通信行为,还会伴有,网站后门-发现后门(Webshell)文件,以及提示网站后门-一句话webshell的安全提示,但是大部分都是单独服务器ECS的用户,具体被阿里云提示的截图如下:
阿里云提示网站后门发现后门(Webshell)文件的解决办法
|
7月前
|
运维 安全 搜索推荐
基于桌面管理实现企业终端安全标准化运维的实战指南
2024年某制造企业因员工私装恶意截图工具致研发数据险遭泄露,暴露终端管理短板:环境混乱、软件失控、运维低效。桌面管理方案实现资产全可视、配置基线固化、软件白名单管控与远程智能运维,助力企业从“救火式”转向“预防式”安全运营。
基于桌面管理实现企业终端安全标准化运维的实战指南
|
6月前
|
人工智能 自然语言处理
阿里云万小智AI建站“灵感值”是什么?灵感值有什么用?如何收费?
阿里云万小智AI建站2.0推出“灵感值”——AI功能统一计费单位(1点=0.01元),覆盖图片生成、多语言翻译、AI客服等。各版本赠送600–15000点,首用还送2000点体验;不足可按10元/1000点灵活充值,3个月有效。阿里云万小智官网:https://t.aliyun.com/U/FmBHHe
503 0
|
6月前
|
XML 人工智能 JSON
别再让 AI 自动总结 Skill 了!
本文作者姜饼,详解如何将重复流程拆解为真正可复用的Skill。指出AI自动总结易生成冗长Prompt,而非结构化Skill;强调Skill应是含入口(SKILL.md)、资料(references/)和脚本(scripts/)的工作目录,并明确各部分职责。附5步拆解法与自查清单,助你告别“伪Skill”。
552 0
|
8月前
|
消息中间件 运维 安全
非得显卡?小模型跑在CPU上也照样快
Aether项目聚焦边缘/无GPU/私有化场景,用≤9B小模型构建高可用智能运维Agent:融合RAG知识库、分级意图路由、SOP式Skill编排与LoRA微调,兼顾数据安全、低资源消耗与强领域专业性。(238字)
1041 2
|
9月前
|
存储 数据采集 人工智能
|
算法 计算机视觉
非极大值抑制详细原理(NMS含代码及详细注释)
非极大值抑制(Non-Maximum Suppression,NMS)详细原理(含代码及详细注释)
3379 1
非极大值抑制详细原理(NMS含代码及详细注释)
|
存储 域名解析 Kubernetes
从零开始,在 Kubernetes 上玩转 Erda(二)
本章节介绍 Erda 的部署以及配置细节
2468 0
从零开始,在 Kubernetes 上玩转 Erda(二)