输入范围滑块,用图像替换拇指不起作用

2022-01-24 00:00:00 slider html css

我正在尝试使用 css 创建自定义范围滑块.

<style>.幻灯片容器{宽度:300px;}.滑块{-webkit 外观:无;宽度:100%;高度:5px;边框半径:5px;背景:#FFFFFF;大纲:无;不透明度:0.7;-webkit-transition:.2s;过渡:不透明度 .2s;}.滑块:悬停{不透明度:1;}.slider::-webkit-slider-thumb {-webkit 外观:无;外观:无;宽度:23px;高度:24px;边框:0;边界半径:50%;背景:#DAA521;光标:指针;}.slider::-moz-range-thumb {宽度:23px;高度:24px;边框:0;边界半径:50%;背景:#DAA521;光标:指针;}</风格>

还有html:

我想将 background: #DAA521; 替换为 background: url('{% static 'images/coffee.png' %}'); 以便拇指变成了图片,而不仅仅是一个彩色圆圈.但是在实践中,这似乎不起作用.有什么想法吗?

解决方案

尝试使用详细的background-*规则.

  • background-image: url('图片的url') - 这是图片
  • background-size: contains; - 确保图像始终包含在按钮内
  • background-position: center center; - 图片总是在按钮的中心
  • background-repeat: no-repeat; - 图片不重复

只需使用静态图像的相对或绝对路径,然后删除 Django 模板代码.将 css 包含到模板中通常不是一个好习惯,直接使用浏览器加载它们.

.slide-container {宽度:300px;}.滑块{-webkit 外观:无;宽度:100%;高度:5px;边框半径:5px;背景:#FFFFFF;大纲:无;不透明度:0.7;-webkit-transition:.2s;过渡:不透明度 .2s;}.滑块:悬停{不透明度:1;}.slider::-webkit-slider-thumb {-webkit 外观:无;外观:无;宽度:23px;高度:24px;边框:0;边界半径:50%;背景图片: url('https://img.icons8.com/material-outlined/344/average-2.png');背景尺寸:包含;背景位置:中心中心;背景重复:不重复;光标:指针;}.slider::-moz-range-thumb {宽度:23px;高度:24px;边框:0;边界半径:50%;背景图片: url('https://img.icons8.com/material-outlined/344/average-2.png');背景尺寸:包含;背景位置:中心中心;背景重复:不重复;光标:指针;}

<div class="slide-container"><input type="range" min="1" max="100" value="50" class="slider" id="myRange"></div>

I am trying to create a custom range slider using css.

<style>
.slide-container {
  width: 300px;
}

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

.slider:hover {
  opacity: 1;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 23px;
  height: 24px;
  border: 0;
    border-radius: 50%;
  background: #DAA521;
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 23px;
  height: 24px;
  border: 0;
    border-radius: 50%;
  background: #DAA521;
  cursor: pointer;
}

    </style>

And the html:

<div class="slide-container">
     <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>

I would like to replace background: #DAA521; with background: url('{% static 'images/coffee.png' %}'); so that the thumb becomes a picture instead of just a coloured circle. However in practice, this doesn't seem to work. Any thoughts?

解决方案

Try to use the detailed background-* rules.

  • background-image: url('the url to the image') - this is the image
  • background-size: contain; - makes sure the image is always contained inside the button
  • background-position: center center; - image is always in the center of the button
  • background-repeat: no-repeat; - image is not repeated

Just use relative or absolute path to your static image, and drop the Django template code. It's generally not a good practice to include css into your templates, load them directly with the browser.

.slide-container {
  width: 300px;
}

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

.slider:hover {
  opacity: 1;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 23px;
  height: 24px;
  border: 0;
    border-radius: 50%;
  background-image: url('https://img.icons8.com/material-outlined/344/average-2.png');
  background-size: contain;
  background-position: center center;
  background-repeat: no-repeat;
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 23px;
  height: 24px;
  border: 0;
    border-radius: 50%;
  background-image: url('https://img.icons8.com/material-outlined/344/average-2.png');
  background-size: contain;
  background-position: center center;
  background-repeat: no-repeat;
  cursor: pointer;
}

<div class="slide-container">
     <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>

相关文章