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;
    },
  }
}
相关推荐
counterxing42 分钟前
我整理了一个免费开发资源目录,还做成了 CLI 和 MCP
前端·agent·ai编程
Javatutouhouduan6 小时前
2026Java面试的正确打开方式!
java·高并发·java面试·java面试题·后端开发·java编程·java八股文
JAVA面经实录9176 小时前
Java初级最终完整版学习路线图
java·spring·eclipse·maven
子兮曰7 小时前
Bun v1.3.14 深度解析:Image API、HTTP/3、全局虚拟存储与五十项变革
前端·后端·bun
Cat_Rocky7 小时前
k8s-持久化存储,粗浅学习
java·学习·kubernetes
知识领航员8 小时前
蘑兔AI音乐深度实测:功能拆解、实测表现与适用场景
java·c语言·c++·人工智能·python·算法·github
释怀°Believe8 小时前
Spring解析
java·后端·spring
kyriewen8 小时前
今天,百年巨头一次砍了9200人,而一个离职科学家的实话让全网睡不着觉
前端·openai·ai编程
ooseabiscuit8 小时前
Laravel4.x:现代PHP框架的奠基之作
java·开发语言·php
问心无愧05139 小时前
ctf show web 入门42
android·前端·android studio