vue项目:自定义滑块过渡效果

html部分

html 复制代码
<div class="slider-main">
    <div class="slider">
      <span v-show="value !== ''" class="pointer" :style="`background-color: ${pointerColor}; left: ${pointerLeft}%`" />
    </div>
    <div class="data-text">
      <span class="min-max">{{ max }}%</span>
      <span class="min-max">{{ min }}%</span>
    </div>
  </div>

css代码:

javascript 复制代码
 .slider-main {
    width: 100%;
    .slider {
      flex: 1;
      height: 14px;
      border-radius: 20px;
      background: linear-gradient(to right,  #F59292, #95D8A4);
      position: relative;
      rotate: 180deg;
      .pointer {
        position: absolute;
        width: 24px;
        height: 35px;
        top: 50%;
        transform: translate(-50%, -50%);
        
        border-radius: 24px;
        border: 3px solid #fff;
        left: 50%;
      }
    }
    .data-text {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .min-max {
        font-size: 12px;
        color: #666;
        margin: 0 5px;
      }
    }
    
  }

js部分

javascript 复制代码
export default {
  props: {
    value: {
      type: [Number, String],
      default: ''
    },
    min: {
      type: Number,
      default: -100
    },
    max: {
      type: Number,
      default: 100
    },
  },
  computed: {
    pointerColor () {
      // 获取当前值对应的颜色
      // return this.colorToHex(currentColor);
      return tools.valueToColor(this.min, this.max, this.value);
    },
    pointerLeft () {
      return this.calculateLeftPosition(this.value, this.min, this.max)
    }
  },
  methods: {
    // 计算当前的left的位置
    calculateLeftPosition(actualValue, minValue, maxValue) {
      // 确保实际值不小于最小值,不大于最大值
      actualValue = Math.max(minValue, Math.min(maxValue, actualValue));
      // 计算left值
      var left = (actualValue - minValue) / (maxValue - minValue) * 100;
      // 返回left值
      return left;
    },
  }
}
相关推荐
剑挑星河月3 小时前
31.下一个排列
java·算法·leetcode
ch.ju3 小时前
Java Programming Chapter 4——Private attribute
java·开发语言
许彰午3 小时前
12_ArrayList与LinkedList深度对比
java·前端·python
lichenyang4533 小时前
鸿蒙练习 12:Provider/Consumer 跨层共享 + HAR 多模块拆分
前端
C+++Python3 小时前
如何在 Java 中使用 BIO、NIO 和 AIO?
java·开发语言·nio
Kurisu5753 小时前
深度拆解:从令牌桶到滑动窗口,高并发系统限流算法的数学本质与边界
java·网络·算法
朱涛的自习室3 小时前
逃离“古法测试”:AI 测试的“三大定律”
android·前端·人工智能
糖果店的幽灵4 小时前
Claude Code 完全实战指南 - 第二章:CLI 命令大全
前端·chrome
repetitiononeoneday4 小时前
【面试题】Redis缓存穿透如何解决?
java·redis·缓存