<input type=‘range‘> css自定义滑块样式

这篇博客展示了如何使用HTML、CSS和JavaScript实现一个自定义样式的滑块,并通过监听onmousemove事件实现实时显示数值的功能。滑块设计包括隐藏原生样式,设置背景颜色,圆角,阴影以及滑过部分的颜色变化。同时,通过JavaScript函数rangeSlide更新滑块值显示。
摘要由CSDN通过智能技术生成

自定义滑块1

在这里插入图片描述

HTML: <input type='range' min='0' max='1000' value='0'>

css:

input{
  width: 400px;
  height: 15px;
  // 不显示原本的样式
  -webkit-appearance: none; 
  background-color: #111;
  outline: none;
  overflow: hidden;
  border-radius: 15px;
  box-shadow: inset 0 0 5px rgba(0,0,0, 1);
}
input::-webkit-slider-thumb{
  -webkit-appearance: none;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: #00fd0a;
  cursor: pointer;
  border: 4px solid #333;
  // 通过巨大的box-shadow 来展示已经滑过的滑条颜色
  // 要配合上面的overflow
  box-shadow: -407px 0 0 400px #00fd0a;
}

JS:

如果说只监听onChange那么只会在拖动停止后才会显示数字,监听onmouseover便可以实时显示数字。

<body>
<div>
<span class="rangeValue">0</span>
<input type="range" name="" value="0" min="0" max="1000" onChange="rangeSlide(value)" onmousemove="rangeSlide(value)">
</div>
<script>
  function rangeSlide(value){
    console.log(value);
    document.querySelector('.rangeValue').textContent = value;
  }
</script>
</body>

全部代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;

    }

    body {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      background-color: #151515;
    }

    div {
      position: relative;
    }

    .rangeValue {
      position: relative;
      display: block;
      text-align: center;
      font-size: 6em;
      color: #999;
    }

    input {
      width: 400px;
      height: 15px;
      -webkit-appearance: none;
      background-color: #111;
      outline: none;
      overflow: hidden;
      border-radius: 15px;
      box-shadow: inset 0 0 5px rgba(0, 0, 0, 1);
    }

    input::-webkit-slider-thumb {
      -webkit-appearance: none;
      width: 15px;
      height: 15px;
      border-radius: 50%;
      background: #00fd0a;
      cursor: pointer;
      border: 4px solid #333;
      box-shadow: -407px 0 0 400px #00fd0a;
    }
  </style>
</head>

<body>
  <div>
    <span class="rangeValue">0</span>
    <input type="range" name="" value="0" min="0" max="1000" onChange="rangeSlide(value)"
      onmousemove="rangeSlide(value)">
  </div>
  <script>
    function rangeSlide(value) {
      console.log(value);
      document.querySelector('.rangeValue').textContent = value;
    }
  </script>
</body>

</html>

笔记依据 YouTube@Online Tutorials的视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值