自定义日历.element-ui 修改时间选择器样式

这篇具有很好参考价值的文章主要介绍了自定义日历.element-ui 修改时间选择器样式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、自定义日历

目录

一、自定义日历

二、时间选择样式自定义


先上效果图

element日历自定义,vue,前端

一、自定义日历组件

element日历自定义,vue,前端

已经封装成vue组件,可选择切换年月:

<template>
  <div class="calendar-container">
    <div class="year flex items-center">
      <el-config-provider :locale="local">
        <el-date-picker
          v-model="selectDate"
          @change="rsetDate"
          type="month"
           class="testDate"
            popper-class="dark_form"
          style="width: 1.2rem"
          :placeholder="nowDate.year + '-' + (nowDate.month + 1)"
        >
        </el-date-picker>
      </el-config-provider>
      <div class="img"></div>
    </div>
    <ul class="week flex">
      <li v-for="(o, index) in 7" :key="o">{{ formatWeek(index) }}</li>
    </ul>
    <ul class="date">
      <li class="none-week dayli"  v-for="o in lastMonthDays" :key="o + 50">
        {{ lastMonthStartDay + o - 1 }}
      </li>
      <!-- 当月 -->
      <span
        @click="clickEvent"
        v-for="day in nowMonthDays"
        :key="day"
        :class="day == nowDate.date ? 'dayli today' : 'dayli'"
      >
        {{ day }}
      </span>

      <li
        
        class="none-week dayli"
        v-for="day in 42 - lastMonthDays - nowMonthDays"
        :key="day + 100"
      >
        {{ day }}
      </li>
    </ul>
  </div>
</template>

<script>
import zhcn from "element-plus/lib/locale/lang/zh-cn";
//  let local = zhcn
export default {
  data() {
    return {
      local: zhcn,
      selectDate: [], //选择日期列表
      nowDate: this.getDate(new Date()), //当前设置时间 默认为当前系统时间
      today: "",
    };
  },
  computed: {
    lastMonthDays() {
      return this.startWeek();
    },
    lastMonthStartDay() {
      return (
        this.calcLastMonthDays(this.nowDate.year, this.nowDate.month) -
        (this.startWeek() - 1)
      );
    },
    nowMonthDays() {
      return this.calcDays(this.nowDate.year, this.nowDate.month);
    },
  },
  created() {
    if (this.setDate) {
      this.nowDate = this.getDate(this.setDate);
      this.settime(this.nowDate)
    }
    // var _this=this;
    // this.today=this.nowDate.date;
    // console.log(this.nowDate)
    // console.log(this.today)
  },
  methods: {
    // 传输时间给父组件 
    settime(){
      let arr=[this.nowDate.year+'-'+(this.nowDate.month+1),this.nowDate.date]// [2022-8,4]
      this.$emit('settime',arr)
    },
    //选择月份
    rsetDate() {
      this.nowDate = this.getDate(this.selectDate);
      this.settime(this.nowDate)
    },
    getDate(date) {
      return {
        year: date.getFullYear(),
        month: date.getMonth(),
        day: date.getDay(),
        date: date.getDate(),
      };
    },
    formatWeek(day) {
      switch (day) {
        case 0:
          return "日";
        case 1:
          return "一";
        case 2:
          return "二";
        case 3:
          return "三";
        case 4:
          return "四";
        case 5:
          return "五";
        case 6:
          return "六";
      }
    },
    //判断闰年
    isLeapYear(year) {
      return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    },
    //根据日子计算星期
    calcWeekend(year, month, day) {
      return new Date(year, month, day).getDay();
    },
    //计算某年某月的天数
    calcDays(year, month) {
      const monthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
      if (this.isLeapYear(year) && month === 1) return 29;
      else return monthDay[month];
    },
    //计算上个月天数
    calcLastMonthDays(year, month) {
      if (month === 0) {
        return this.calcDays(year - 1, 11);
      } else {
        return this.calcDays(year, month - 1);
      }
    },
    //计算当月开始星期
    startWeek() {
      return this.calcWeekend(this.nowDate.year, this.nowDate.month, 1);
    },
    //
    clickEvent(e) {
      let monthNo = this.nowDate.month;
      let month = monthNo <= 11 ? monthNo + 1 : 0;
      let date = {
        year: this.nowDate.year,
        month: month,
        week: new Date(
          this.nowDate.year,
          this.nowDate.month,
          e.target.innerText
        ).getDay(),
        day: Number(e.target.innerText),
      };
      // this.$emit("click-event", date);
      this.nowDate.date=date.day
      console.log(date.day);
      this.settime(this.nowDate)
    },
  },
};
</script>


<style  lang="scss" scoped>
.calendar-container {
  background: rgba(7, 25, 61, 0.39);
  border-radius: 4px;
  padding: 16px;
}
.year {
  position: relative;

  ::v-deep {
    .el-input__wrapper {
      border-radius: 4px;
      border: 1px solid #818ea9;
      box-shadow: none;
      margin-bottom: 16px;
    }
    .el-input__prefix {
      width: 33px;
    }
  }
  .img {
    position: absolute;
    top: 1.5px;
    z-index: 888;
    width: 33px;
    height: 27px;
    margin: 2px;
    // margin-right: 16px;
    background-image: url("~@/assets/imgs/landedEstate/public-resources/time.png");
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
  p {
    font-family: "HarmonyOS Sans";
    font-style: normal;
    font-weight: 400;
    font-size: 16px;
    line-height: 19px;
    color: #ffffff;
    margin-right: 12px;
  }
}

.week {
  padding: 8px;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  height: 40px;
  background: linear-gradient(
    270deg,
    rgba(19, 30, 52, 0.308) 0%,
    rgba(19, 30, 52, 0.7) 100%
  );
  border-radius: 4px;
  border-radius: 4px;
  margin-bottom: 12px;

  li {
    height: 24px;
    width: 32px;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    display: flex;
    flex-wrap: wrap;
    list-style: none;
    align-content: center;
    font-family: "HarmonyOS Sans";
    font-style: normal;
    font-weight: 400;
    font-size: 16px;
    line-height: 20px;
    text-align: center;
    letter-spacing: 0.02em;
  }
}
.date {
  justify-content: center;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  margin-top: 0;
  .dayli:nth-child(7n) {
    border-radius: 0px 2px 2px 0;
    // color: #22ff47;
  }
  .dayli:nth-child(7n + 1) {
    border-radius: 2px 0px 0px 2px;
    // color: #22ff47;
  }
  span {
    border-bottom: 1px;
    border-top: 1px;
    border-color: #818ea9;
    border-style: solid;
  }
  span:last-of-type {
    border-radius: 0px 2px 2px 0px;
    // color: #22ff47;
    border-right: 1px;
    border-color: #818ea9;
    border-style: solid;
  }
  span:first-of-type {
    border-radius: 0px 2px 2px 0px;
    // color: #ff229c;
    border-left: 1px;
    border-color: #818ea9;
    border-style: solid;
  }

  .today {
    background: linear-gradient(
      112.08deg,
      #8ab1ff 0.45%,
      #5985dd 61.64%,
      #3f6dcb 95.27%
    );
    box-shadow: -1px 3px 4px rgba(124, 149, 219, 0.6);
    border-radius: 4px;
    font-family: "HarmonyOS Sans";
    font-style: italic;
    font-weight: bolder;
    font-size: 16px;
    line-height: 20px;
    /* identical to box height, or 125% */

    text-align: center;
    letter-spacing: 0.02em;

    color: #ffffff;

    text-shadow: 0px 2px 4px #2366e9;
  }
  li,
  span {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 8px;
    width: 43px;
    height: 35px;
    text-align: center;
    font-family: "HarmonyOS Sans";
    font-style: normal;
    font-weight: 400;
    font-size: 16px;
    line-height: 20px;
    text-align: center;
    letter-spacing: 0.02em;
    color: #ffffff;
    background: #131e34b3;
  }
  li:hover {
    background: linear-gradient(
      112.08deg,
      #8ab1ff 0.45%,
      #5985dd 61.64%,
      #3f6dcb 95.27%
    );
    box-shadow: -1px 3px 4px rgba(124, 149, 219, 0.6);
    border-radius: 4px;
    cursor: pointer;
  }

  .none-week {
    font-family: "HarmonyOS Sans";
    font-style: normal;
    font-weight: 400;
    font-size: 16px;
    line-height: 20px;

    text-align: center;
    letter-spacing: 0.02em;

    color: #ffffff;

    opacity: 0.4;
  }
}
</style>

二、时间选择样式自定义

element日历自定义,vue,前端

 因 element-UI的时间选择器 el-date-picker 是将元素直接挂载到页面的<body>中,而非自身元素下,所以使用 /deep/、 >>>、 ::v-deep 等穿透无法定位到元素。

解决方案:

利用时间选择器的popper-class属性,设置样式。

有2种方式:

  1. 创建style.scss,在该文件里进行样式的编写,再引入
  2. 在APP.vue中加入样式的编写(根文件)
        <el-date-picker
          type="month"
          popper-class="dark_form" >
        </el-date-picker>

//效果图对应样式



//   日期选择弹窗样式
  .el-picker__popper.el-popper[role="tooltip"] {
    border-color: #24386000;
  }
  .el-popper.is-light .el-popper__arrow::before {
    background: #131e34;
    border: none !important;
  }
  .dark_form {
    background: #131e34 !important;
    border: #131e34;
    .el-date-picker__header--bordered {
      border-bottom: solid 0.01rem #818ea9;
    }
    .el-picker__popper.el-popper[role="tooltip"] .el-popper__arrow::before {
      border: none !important;
    }
    .el-picker-panel__body {
      background: #131e34 !important;
    }
    .el-date-picker__header-label {
      color: #fbfbfb;
      font-family: "HarmonyOS Sans";
    }
    .el-picker-panel__icon-btn {
      color: #fbfbfb;
      font-family: "HarmonyOS Sans";
    }
    .el-month-table td .cell {
      color: #fbfbfb;
      font-family: "HarmonyOS Sans";
    }
	.el-year-table td .cell {
      color: #fbfbfb;
      font-family: "HarmonyOS Sans";
}
  }

搜索

复制文章来源地址https://www.toymoban.com/news/detail-520026.html

到了这里,关于自定义日历.element-ui 修改时间选择器样式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • element-ui折叠面板怎么修改样式

    修改前 因为组件封装,要使用样式穿透来修改… 注意需要把需要样式穿透的类单独拿出来,不能包裹在scss格式的类里了 修改后 展开… 最后这个缝隙可以用边框来填补,至于伪类加横杠最好不要用,因为展开和折叠的时候会出现一瞬间白线

    2024年02月12日
    浏览(40)
  • element-ui 自定义loading加载样式

    element-ui 中的 loading 加载功能,默认是全屏加载效果, 设置局部,需要自定义样式,自定义的方法如下:

    2024年02月11日
    浏览(37)
  • 自定义element-ui走马灯(轮播)样式

    自定义el-carousel-item指示器样式 把指示器变成圆点 效果图:  

    2024年02月13日
    浏览(32)
  • element-ui 时间日期选择器限制选择范围

    组件代码 场景1:设置选择今天及今天之后的日期 情景2: 设置选择今天以及今天以前的日期 情景3: 设置选择三个月之内到今天的日期 情景4: 设置选择最大范围为30天

    2024年02月12日
    浏览(27)
  • element-ui日期选择器时间差

    #主要记录三个问题 日期选择器选择时获取到的格式相差八小时 当日期格式为–拼接时,转成时间戳会相差八小时(2023-03-09) DatePicker设置区域范围和校验(暂无,明天加上) 由于element-ui日期选择器用的时间为世界标准时间,我们国家的标准时间为东八区,所以会有8小时时间

    2024年02月08日
    浏览(36)
  • element-ui form表单自定义label的样式、内容

    element-ui form表单自定义label的样式、内容

    2024年04月17日
    浏览(28)
  • vue修改element-ui日期下拉框datetimePicker的背景色样式

    在vue项目中,源datetimePicker的背景样式,往往与项目背景不搭,需要修改。   1.先在assets里面新建一个index.css文件来存储全局样式 2.在main.js里面导入这个css文件最后在里面加入我们想要的样式 此时的效果如下图   3.在el-date-picker中设置样式 4.设置对应的背景样式 得到下图效果

    2024年02月11日
    浏览(40)
  • element-ui中 this.$confirm修改确定取消按钮顺序和样式

    首先其他博客上说是给 this.$confirm中添加取消样式,我试过了无效

    2024年02月14日
    浏览(29)
  • 【UI】 修改element-ui input输入框placeholder提示信息、占位符的样式

    如何修改 代码如下: 效果 案例展示 vue2.x代码 案例效果

    2024年02月15日
    浏览(31)
  • element-ui时间选择器(DatePicker )数据回显

    目录 前言 一、element-ui时间选择器(DatePicker )是什么? DatePicker 日期选择器 二、返回数据格式 1.引入 总结 需求: element-ui时间选择器(DatePicker )数据回显,后台返回数据时间,然后回显到前台展示。 效果:   DatePicker 日期选择器 用于选择或输入日期 https://element.eleme.c

    2024年01月19日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包