elementplus组件中el-calendar组件自定义日期单元格内容及样式

一、界面结构编码
html 复制代码
<el-calendar ref="calendarRef">
  // 日历头
  <template #header="{ date }">
    <div class="calendarLegend">
      <div class="item niandu">
        <div class="flag"></div>
        <div class="content">A</div>
      </div>
      <div class="item guncuo">
        <div class="flag"></div>
        <div class="content">B</div>
      </div>
      <div class="item xianhuo">
        <div class="flag"></div>
        <div class="content">C</div>
      </div>
    </div>
    <div class="button-group">
      <el-button>
        <el-icon style="margin-right: 6px">
          <d-arrow-left />
        </el-icon>
        上个月</el-button
      >
      <div>{{ date }}</div>
      <el-button
        >下个月
        <el-icon style="margin-left: 6px">
          <d-arrow-right />
        </el-icon>
      </el-button>
      <el-button type="primary" style="padding: 0 25px">跳转到今天</el-button>
    </div>
  </template> 
  // 具体日历内容
  <template #date-cell="{ data }">
    <div class="custom-date-cell" :class="{ 'is-selected': data.isSelected }" style="padding: 5px">
      <span
        class="date-day"
        :class="{
          'is-selected': data.isSelected,
          'not-in-month': !data.inRange,
          'is-today': isToday(data.day),
        }"
      >
        {{ formatDate(data.day, "DD") }}
      </span>
      <!-- 日期单元格需要填的内容 -->
      今天是:{{ data.day }}日
    </div>
  </template>
</el-calendar>
二、样式编码
css 复制代码
.calendarLegend {
  display: flex;
  justify-content: flex-start;
  gap: 30px;
  .item {
    display: flex;
    justify-content: flex-start;
    gap: 10px;
    align-items: center;
    .flag {
      width: 8px;
      height: 8px;
      border-radius: 50%;
    }
    &.niandu {
      .flag {
        background: #15e474;
        box-shadow: 0px 0px 10px 0px #15e474;
      }
    }
    &.xianhuo {
      .flag {
        background: #a366ff;
        box-shadow: 0px 0px 10px 0px #9046ff;
      }
    }
    &.guncuo {
      .flag {
        background: #2ca7ff;
        box-shadow: 0px 0px 10px 0px #4680ff;
      }
    }
  }
}

.button-group {
  ::v-deep(.el-button:not(:first-child)) {
    margin-left: 8px;
  }
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 10px;
}
::v-deep(.el-calendar-day) {
  // height: 175px !important;
  height: 150px !important;
  // overflow: hidden;
  .custom-date-cell {
    width: 100%;
    height: 100%;
    .date-day {
      display: inline-block;
      width: 26px;
      height: 26px;
      line-height: 26px;
      text-align: center;
      border-radius: 50%;
      background-color: transparent;
      // color: #fff;
      font-size: 16px;
    }
    // 今天的日历单元格,相关样式。is-today为动态添加的类名
    .is-today {
      background-color: #4680ff;
      color: #fff;
    }

    .dealDetail {
      display: flex;
      flex-direction: column;
      gap: 3px;
      margin-top: 3px;
      // height: 110px !important;
      height: 105px !important;
      overflow-y: auto !important;
      .item {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        gap: 5px;
        height: 24px;
        line-height: 24px;
        font-size: 12px;
        border-radius: 4px;
        padding-left: 5px;
        // 日期单元格的信息,超出显示省略号
        .content {
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .flag {
          width: 8px;
          height: 8px;
          border-radius: 50%;
          flex-shrink: 0; // 阻止弹性容器在空间不足时被压缩,保留原始尺寸
        }
        &.xianhuo {
          color: #bfa2ff;
          background: rgba(142, 78, 255, 0.3);
          border: 1px solid #a682f7;
          .flag {
            background: #a366ff;
            box-shadow: 0px 0px 10px 0px #9046ff;
          }
        }
        &.guncuo {
          color: #7ab8fa;
          background: rgba(23, 150, 255, 0.3);
          border: 1px solid #459fff;
          .flag {
            background: #2ca7ff;
            box-shadow: 0px 0px 10px 0px #6d9bff;
          }
        }
        &.niandu {
          color: #1bd871;
          background: rgba(30, 178, 89, 0.3);
          border: 1px solid #0bb552;
          .flag {
            background: #15e474;
            box-shadow: 0px 0px 10px 0px #15e474;
          }
        }
      }
    }
  }
  // 经过单元格时样式
  &:hover {
    // border: 3px solid #229fff;
    background: rgba(70, 128, 255, 0.16) !important;
    box-shadow: inset 0px 0px 10px 0px rgba(70, 128, 255, 0.6);
  }
}

.calendarContent {
  display: flex;
  justify-content: flex-start;
  .right {
    width: 119px;
  }
}

::v-deep(.el-calendar) {
  --el-calendar-selected-bg-color: rgba(70, 128, 255, 0.16) !important;
  thead {
    display: none;
  }
}

::v-deep(.el-calendar__header) {
  // border-top: 1px solid rgba(255, 255, 255, 0.16);
  border-bottom: 1px solid rgba(255, 255, 255, 0.16);
}
// 日期单元格样式
::v-deep(.el-calendar-day) {
  border: 1px solid #748ab8;
  box-shadow: inset 0px 0px 10px 0px rgba(70, 128, 255, 0.6);
  opacity: 0.2 !important;
  padding: 0;
}
// 当月单元格
::v-deep(.el-calendar-table__row td.current .el-calendar-day) {
  border: 1px solid #748ab8;
  box-shadow: inset 0px 0px 10px 0px rgba(70, 128, 255, 0.6);
  opacity: 1 !important;
}
三、界面效果
相关推荐
Revolution611 小时前
一段 JavaScript 代码执行时,到底发生了什么
前端·javascript
To_OC10 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
触底反弹11 小时前
一文搞懂 Tailwind CSS 弹性布局:从原理到实战
前端·css·html
To_OC12 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
小林ixn12 小时前
从 ??= 到 onKeyDown:一个 React 组件的“自我修养”
前端·javascript·react.js
এ慕ོ冬℘゜13 小时前
前端实战:jQuery 多条件联合搜索(标题模糊查询 + 日历时间段筛选)
前端·javascript·jquery
随心点儿13 小时前
js postMessage 实现跨域发送消息-应用示例
javascript·ecmascript·postmessage
kyriewen15 小时前
我让AI改一个bug——它偷偷动了5个我没让它碰的地方
前端·javascript·ai编程
小白巨白15 小时前
玫瑰花园管理系统:AI识病+3D可视化,一套面向中小型玫瑰种植园的数字化管理工具
css·人工智能·计算机视觉·html5
gis开发之家19 小时前
《Vue3 从入门到大神35篇》Vue3 源码详解(五):effect 依赖收集原理——track 与 trigger 是如何工作的?
javascript·typescript·前端框架·vue3·vue3源码