微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

即使我们不是点击按钮而是点击轨道,如何进行更改?

如何解决即使我们不是点击按钮而是点击轨道,如何进行更改?

您点击轨道,将 offsetX 转换为百分比并将其与最小值和最大值进行比较

您点击轨道,将 offsetX 转换为百分比并将其与最小值和最大值进行比较

var inputLeft = document.getElementById("input-left");
var inputRight = document.getElementById("input-right");

var minValue = document.querySelector('.min');
var maxValue = document.querySelector('.max');

var thumbLeft = document.querySelector(".slider > .thumb.left");
var thumbRight = document.querySelector(".slider > .thumb.right");
var range = document.querySelector(".slider > .range");

function setLeftValue() {
  var _this = inputLeft,min = parseInt(_this.min),max = parseInt(_this.max);

  _this.value = Math.min(parseInt(_this.value),parseInt(inputRight.value) - 1);

  var percent = ((_this.value - min) / (max - min)) * 100;

  thumbLeft.style.left = percent + "%";
  range.style.left = percent + "%";
}
setLeftValue();

function setRightValue() {
  var _this = inputRight,max = parseInt(_this.max);

  _this.value = Math.max(parseInt(_this.value),parseInt(inputLeft.value) + 1);

  var percent = ((_this.value - min) / (max - min)) * 100;

  thumbRight.style.right = (100 - percent) + "%";
  range.style.right = (100 - percent) + "%";
}
setRightValue();

inputLeft.addEventListener("input",setLeftValue);
inputRight.addEventListener("input",setRightValue);

inputLeft.addEventListener('input',function() { updateValue.call(this,minValue) } )
inputRight.addEventListener("input",maxValue) } )


function updateValue (dataElem){
  dataElem.textContent = this.value;
}
.middle {
  position: relative;
  width: 50%;
  max-width: 500px;
  margin-top: 55px;
}

.middle .slider {
  position: relative;
  z-index: 1;
  height: 10px;
  margin: 0 15px;
}

.middle .slider>.track {
  position: absolute;
  z-index: 1;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  border-radius: 5px;
  background-color: #c6aee7;
}

.middle .slider>.range {
  position: absolute;
  z-index: 2;
  left: 25%;
  right: 25%;
  top: 0;
  bottom: 0;
  border-radius: 5px;
  background-color: #6200ee;
}

.middle .slider>.thumb {
  position: absolute;
  z-index: 3;
  width: 30px;
  height: 30px;
  background-color: #6200ee;
  border-radius: 50%;
  Box-shadow: 0 0 0 0 rgba(98,238,0.1);
  transition: Box-shadow .3s ease-in-out;
}

.middle .slider>.thumb.left {
  left: 25%;
  transform: translate(-15px,-10px);
}

.middle .slider>.thumb.right {
  right: 25%;
  transform: translate(15px,-10px);
}

.middle .slider>.thumb.hover {
  Box-shadow: 0 0 0 20px rgba(98,0.1);
}

.middle .slider>.thumb.active {
  Box-shadow: 0 0 0 40px rgba(98,0.2);
}

.middle input[type=range] {
  position: absolute;
  pointer-events: none;
  -webkit-appearance: none;
  z-index: 2;
  height: 10px;
  width: 100%;
  opacity: 0;
}

.middle input[type=range]::-webkit-slider-thumb {
  pointer-events: all;
  width: 30px;
  height: 30px;
  border-radius: 0;
  border: 0 none;
  background-color: red;
  -webkit-appearance: none;
}

.wrap {
  display: flex;
  justify-content: space-between;
  padding-top: 25px;
}
<div class="middle">
    <div class="multi-range-slider">
        <input type="range" id="input-left" min="18" max="90" value="45">
        <input type="range" id="input-right" min="18" max="90" value="75">

        <div class="slider">
            <div class="track"></div>
            <div class="range"></div>
            <div class="thumb left"></div>
            <div class="thumb right"></div>
        </div>
    </div>
    <div class="wrap">
      <div class="min">45</div>
      <div class="max">75</div>
    </div>
</div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。