element-plus中el-slider默认显示tooltip,自定义样式




1. 样式一:
  •  图示:

  • 代码:
<template>
        <div class="my-slider">
          <el-slider
            :step="0.5"
            v-model="value1"
            :show-tooltip="false"
            :min="min"
            :max="max"
            style="position: relative; z-index: 1"
          >
          </el-slider>
          <div class="my-slider__tooltip" :style="style">
            <el-button class="my-slider__tooltip-wrapper">
              {{ value1 }}
            </el-button>
          </div>
        </div>
</template>
<script setup lang="ts">
import { ref ,computed } from "vue";
const value1 = ref<number>(0.5); // 默认值
const min = ref(-15);
const max = ref(15);


const style = computed(() => {
  const length = max.value - min.value,
    progress = value1.value - min.value,
    left = (progress / length) * 100;
  return {
    paddingLeft: `${left}%`,
  };
});

</script>

<style lang="scss" scoped>

.my-slider {
  width: calc(100% - 92px);
  .my-slider__tooltip {
    .my-slider__tooltip-wrapper {
      height: 32px;
      transform: translateX(-50%);
      top: -50%;
    }
  }
}

</style>
2. 样式二:
  • 图示:

  • 代码:
<template>
  <div class="my-slider">
    <el-slider
      :step="0.5"
      v-model="value1"
      :show-tooltip="false"
      :min="min"
      :max="max"
      style="position: relative; z-index: 1"
    >
    </el-slider>
    <div class="my-slider__tooltip" :style="style">
      <span class="my-slider__tooltip-span">{{ value1 }}</span>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref ,computed } from "vue";
const value1 = ref<number>(0.5); // 默认值
const min = ref(-15);
const max = ref(15);


const style = computed(() => {
const length = max.value - min.value,
progress = value1.value - min.value,
left = (progress / length) * 100;
return {
paddingLeft: `${left}%`,
};
});

</script>

<style lang="scss" scoped>

.my-slider {
  width: calc(100% - 92px);
  .my-slider__tooltip {
    .my-slider__tooltip-wrapper {
      transform: translateX(-50%);
      position: relative;
      top: -32px;
      z-index: 10;
      background: transparent;
      border: none;
    }
    .my-slider__tooltip-span {
      transform: translateX(-50%);
      display: block;
      position: relative;
      width: 28px;
      height: 28px; // 和el-slider__button大小保持一致
      line-height: 28px; 
      z-index: 10;
      bottom: 30px; // 位置偏移加上了border的2个像素
      text-align: center;
      font-size: 12px;
    }
  }
  :deep(.el-slider__button) {
    width: 28px !important;
    height: 28px !important;
  }
}
.el-slider {
  padding: 8px;
  margin: 0;
}
</style>

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值