自定义修改 input 的range

本文介绍如何使用CSS3背景渐变实现自定义颜色的input进度条,包括滑动条背景及操作按钮样式定制,并提供了一个Vue实例演示如何根据输入值动态调整进度条颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最终效果

 遇到的问题:
        1、无法直接修改 input 进度条进度条颜色,解决办法使用css3背景渐变解决

css: 需要注意加上-webkit-appearance: none 否则无效
 /*滑动条背景*/
  input[type="range"] {
    -webkit-appearance: none;
    width: 522px;
    height: 24px;
    background: linear-gradient(90deg, #1C2D4B 0%, #292F3A 100%);
    border-radius: 12px;
    box-shadow: 0px 0px 8px 0px rgba(68,119,255,1);
  }
  input[type="range"]::-ms-fill-lower {
    -webkit-appearance: none;
    background: #f00;
  }
  /*滑动条操作按钮样式*/
  input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 6px;
    height: 24px;
    background: #4477FF;
    box-shadow: 0px 0px 6px 0px rgba(68,119,255,1);
  }

完整案例:

<template>
  <div style="margin: 50px;">
    <input
      v-model="input"
      max="1000"
      :style="{'background': `linear-gradient(to right, rgba(68, 119, 255, 0.2) ${getPercent(input, 1000)}%, #252E3F ${ getPercent(input, 1000)}% `}"
      type="range">
    <span class="c_fff">{{input}}</span>
  </div>
</template>

<script>
export default {
  data () {
    return {
      input: 0
    }
  },
  mounted () {
  },
  methods: {
    getPercent (num, total) {
      num = parseFloat(num)
      total = parseFloat(total)
      if (isNaN(num) || isNaN(total)) {
        return 0
      }
      return total <= 0 ? 0 : (Math.round(num / total * 10000) / 100.00)
    },
  }
}

</script>

<style lang="less">
  /*滑动条背景*/
  input[type="range"] {
    -webkit-appearance: none;
    width: 522px;
    height: 24px;
    background: linear-gradient(90deg, #1C2D4B 0%, #292F3A 100%);
    border-radius: 12px;
    box-shadow: 0px 0px 8px 0px rgba(68,119,255,1);
  }
  input[type="range"]::-ms-fill-lower {
    -webkit-appearance: none;
    background: #f00;
  }
  /*滑动条操作按钮样式*/
  input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 6px;
    height: 24px;
    background: #4477FF;
    box-shadow: 0px 0px 6px 0px rgba(68,119,255,1);
  }
</style>


        

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值