如何更改范围滑块标记?

How to change the range slider marker?

我必须做到这一点: Range Slider

我已经完成了范围滑块部分,但我在更改范围滑块上的标记时遇到问题。即使我提供图像 src :“https://png.icons8.com/metro/52/000000/sort-down.png”。

我正在将我的工作添加到我完成的地方。

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
.slidecontainer {
  width: 100%;
}

.slider {
  width: 30%;
  height: 10px;
  border-radius: 5px;
  background-image: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(0, 128, 0, 1));
  outline: none;
  opacity: 0.7;
  transition: opacity .2s;
  -webkit-appearance: none;
}

.slider:hover {
   background-image: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(0, 128, 0, 1));
}

.slider::-webkit-slider-thumb {
  width: 23px;
  height: 24px;
  border: 0;
  background: url('contrasticon.png');
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 23px;
  height: 24px;
  border: 0;
  background: url('contrasticon.png');
  cursor: pointer;
}
<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  
</div>

您需要在滑块滑块上使用 appearance: none; 才能使用自定义图像。 看看:

.slidecontainer {
  width: 100%;
}

.slider {
  width: 30%;
  height: 10px;
  border-radius: 5px;
  background-image: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(0, 128, 0, 1));
  outline: none;
  opacity: 0.7;
  transition: opacity .2s;
  -webkit-appearance: none;
}

.slider:hover {
   background-image: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(0, 128, 0, 1));
}

.slider::-webkit-slider-thumb {
  width: 23px;
  height: 24px;
  border: 0;
  background: url('https://png.icons8.com/metro/52/000000/sort-down.png');
  background-size: 16px;
  background-repeat: no-repeat;
  background-position: 0 -4px;
  cursor: pointer;
  -webkit-appearance: none;
}

.slider::-moz-range-thumb {
  width: 23px;
  height: 24px;
  border: 0;
  background: url('https://png.icons8.com/metro/52/000000/sort-down.png');
    background-size: 16px;
  background-repeat: no-repeat;
  background-position: 0 -4px;

  cursor: pointer;
  -moz-appearance: none;
}
<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  
</div>

现在可以随意更改图像 属性,如大小、旋转、z-index 等任何您需要的

您可以尝试一个纯粹的 CSS 解决方案,您可以在不需要图像的情况下轻松控制箭头(大小、颜色、位置):

.slider {
  margin:50px;
  width: 30%;
  height: 10px;
  border-radius: 5px;
  background-image: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(0, 128, 0, 1));
  outline: none;
  opacity: 0.7;
  transition: opacity .2s;
  -webkit-appearance: none;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 40px;
  background:
    linear-gradient(to bottom left,#000 49%,transparent 50%) top left/50% 15px,
    linear-gradient(to bottom right,#000 49%,transparent 50%) top right/50% 15px;
  background-repeat:no-repeat;
  display:inline-block;
}

.slider::-moz-range-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 40px;
  background:
    linear-gradient(to bottom left,#000 49%,transparent 50%) top left/50% 15px,
    linear-gradient(to bottom right,#000 49%,transparent 50%) top right/50% 15px;
  background-repeat:no-repeat;
  display:inline-block;
}
<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  
</div>