[英]How to rotate input type range thumb in javascript?
I have an <input type="range">
and want to rotate the thumb - but only the thumb on input. 我有一个
<input type="range">
并想要旋转拇指 - 但只有拇指输入。
My problem is: When i try to rotate it, it rotates the whole slider. 我的问题是:当我尝试旋转它时,它会旋转整个滑块。
Can someone help me? 有人能帮我吗?
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>
Since you cannot target pseudo-element using JS, consider using CSS variable. 由于您无法使用JS来定位伪元素,因此请考虑使用CSS变量。 You define them within the input element and they are inherited by the thumb;
你在input元素中定义它们,它们由拇指继承 ; 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.