利用Element UI的滑块Slider实现刻度尺

一、效果图
在这里插入图片描述
二、一些要点说明
①首先需要了解slider的一些参数,尤其需要利用marks来实现刻度:
在这里插入图片描述
②实现marks的方法需要用到this.$createElement,所以也需要了解它的写法:this.$createElement()方法
③需要改变slider的一些css以实现刻度尺效果:
在这里插入图片描述
三、实现过程
①安装element ui:element ui的安装使用步骤
②在要使用刻度尺的地方添加:

<el-slider class="self_slider" v-model="count" :max="countMax" :min="countMin" :marks="guideMarks(countMin, countMax)" :show-tooltip="false" show-input></el-slider>

③在data中定义相关变量:

count: 0,
countMin: 0,
countMax: 70

④实现guideMarks()方法:

    guideMarks (min, max) {
      const marks = {}
      for (let i = 0; i <= 160; i++) {
        if ((i === min && i % 10 !== 0 && i > 9) || (i === max && i % 10 !== 0 && i > 9)) {
          marks[i] = {
            style: {
              color: '#000'
            },
            label: this.$createElement('div', { class: 'marks' }, [
              this.$createElement('div', '|'),
              this.$createElement('div', { class: 'marks-line' }, '|'),
              this.$createElement('div', { class: 'marks-line2' }, '|'),
              this.$createElement('div', { class: 'marks-value' }, i)
            ])
          }
        } else if ((i === min && i % 10 !== 0 && i < 9) || (i === max && i % 10 !== 0 && i < 9)) {
          marks[i] = {
            style: {
              color: '#000'
            },
            label: this.$createElement('div', { class: 'marks' }, [
              this.$createElement('div', '|'),
              this.$createElement('div', { class: 'marks-line' }, '|'),
              this.$createElement('div', { class: 'marks-line2' }, '|'),
              this.$createElement('div', { class: 'marks-value0' }, i)
            ])
          }
        } else if (i % 10 === 0) {
          // marks[i] = i + ''
          if (i === 0) {
            marks[i] = {
              style: {
                color: '#000'
              },
              label: this.$createElement('div', { class: 'marks' }, [
                this.$createElement('div', '|'),
                this.$createElement('div', { class: 'marks-line' }, '|'),
                this.$createElement('div', { class: 'marks-line2' }, '|'),
                this.$createElement('div', { class: 'marks-value0' }, i)
              ])
            }
          } else {
            marks[i] = {
              style: {
                color: '#000'
              },
              label: this.$createElement('div', { class: 'marks' }, [
                this.$createElement('div', '|'),
                this.$createElement('div', { class: 'marks-line' }, '|'),
                this.$createElement('div', { class: 'marks-line2' }, '|'),
                this.$createElement('div', { class: 'marks-value' }, i)
              ])
            }
          }
        } else if (i % 1 === 0) {
          marks[i] = {
            style: {
              color: '#ccc'
            },
            // label: this.$createElement('span', '|')
            label: this.$createElement('div', { class: 'marks' }, [
              this.$createElement('div', '|'),
              this.$createElement('div', { class: 'marks-line' }, '|')
            ])
          }
        }
      }
      return marks
    }

⑤相关css:

<style scoped lang="css">
.self_slider {
  width: 90%;
  margin: 20px auto 0;
  height: 90px;
}
.el-slider__marks-text span {
  display: inline-block;
  height: 17px !important;
  overflow: hidden;
}
.marks {
  text-align: center;
  position: relative;
}
.marks-line {
  position: absolute;
  top: -12px;
}
.marks-line2 {
  position: absolute;
  top: -24px;
}
.marks-value {
  position: absolute;
  top: -40px;
  right: -7px;
}
.marks-value0 {
  position: absolute;
  top: -40px;
  right: -2px;
}
</style>
<style>
.el-slider__runway {
  height: 0px;
}
.el-slider__bar {
  height: 0px;
}
.el-slider__button-wrapper {
  top: 22px;
}
.el-slider__button {
  border: none;
  background-color: transparent;
}
.el-slider__button::after, .el-slider__button.dragging::after {
  content: "";
  display: block;
  position: absolute;
  width: 0;
  height: 0;
  border: 6px solid transparent;
  border-bottom-color: #FF9901;
  top: 3px;
  left: 12px;
}
.el-slider__button-wrapper .el-tooltip, .el-slider__button-wrapper::after {
  display: block;
}
.el-slider__button.hover, .el-slider__button.dragging {
  transform: scale(1.0);
}
.el-slider__input {
  margin-top: 3px;
  width: 50%;
  position: absolute;
  right: 25%;
  top: 170px;
}
.el-slider__runway.show-input {
  margin-right: 0;
  width: auto;
}
</style>
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值