How to rotate input type range thumb in javascript?

試著忘記壹切 提交于 2019-12-03 21:16:22

Since you cannot target pseudo-element using JS, consider using CSS variable. You define them within the input element and they are inherited by the thumb; thus you can easily achieve what you want.

function slide(event) {
  event.target.style.setProperty('--r',event.target.value+'deg');
}
.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));
}
<div class="slidecontainer">
  <input type="range" min="0" max="360" value="180" class="slider" oninput="slide(event)">
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!