如何在javascript中旋转输入类型范围拇指?

时间:2018-04-20 13:35:12

标签: javascript html css css3 css-transforms

我有一个<input type="range">,想要旋转拇指 - 但只需输入拇指。

我的问题是:当我尝试旋转它时,它会旋转整个滑块。

有人可以帮助我吗?

function slide(event){
  event.target.style.webkitTransform = 'rotate(20deg)';
}
        .slidecontainer {
            width: 100%;
        }
        .slider {
            -webkit-appearance: none;
            appearance: none;
            width: 100%;
            height: 20px;
            background: #d3d3d3;
            outline: none;
            opacity: 0.7;
            -webkit-transition: .2s;
            transition: opacity .2s;
            border-radius: 20px;
        }
        .slider:hover {
            opacity: 1;
        }
        .slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 50px;
            height: 50px;
            border-radius: 50px;
            background: grey;
            background-image: url(img/thumb.png);
            background-size: 35px 35px;
            background-position: center;
            background-repeat: no-repeat;
            cursor: pointer;
        }
        .slider::-moz-range-thumb {
            width: 50px;
            height: 50px;
            border-radius: 50px;
            background: grey;
            background-image: url(img/thumb.png);
            background-size: 35px 35px;
            background-position: center;
            background-repeat: no-repeat;
            cursor: pointer;
        }
<div class="slidecontainer">
  <input type="range" min="100" max="500" value="300" class="slider" oninput="slide(event)">
</div>

1 个答案:

答案 0 :(得分:8)

由于您无法使用JS来定位伪元素,因此请考虑使用CSS变量。您可以在输入元素中定义它们,并通过拇指继承;因此,您可以轻松实现您想要的目标。

&#13;
&#13;
function slide(event) {
  event.target.style.setProperty('--r',event.target.value+'deg');
}
&#13;
.slidecontainer {
  width: 100%;
}

.slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 20px;
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  transition: opacity .2s;
  border-radius: 20px;
}

.slider:hover {
  opacity: 1;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 50px;
  height: 50px;
  border-radius: 50px;
  background:linear-gradient(red 50%,blue 0);
  cursor: pointer;
  display:inline-block;
  transform:rotate(var(--r,180deg));
}

.slider::-moz-range-thumb {
  width: 50px;
  height: 50px;
  border-radius: 50px;
  background: linear-gradient(red 50%,blue 0);
  cursor: pointer;
  display:inline-block;
  transform:rotate(var(--r,180deg));
}
&#13;
<div class="slidecontainer">
  <input type="range" min="0" max="360" value="180" class="slider" oninput="slide(event)">
</div>
&#13;
&#13;
&#13;