【问题标题】:Input range slider, replace thumb with image not working输入范围滑块,用图像替换拇指不起作用
【发布时间】:2020-07-10 12:18:55
【问题描述】:

我正在尝试使用 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>

还有html:

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

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

【问题讨论】:

    标签: html css slider


    【解决方案1】:

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

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

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

    .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>

    【讨论】:

      【解决方案2】:

      将背景:#DAA521 替换为 background-image: url("path to your image file");

      【讨论】:

      • 是的,这就是我正在尝试的,但不幸的是它不起作用
      • 根据您的问题,您只使用 "background" 作为属性,而不是它应该是 "background-image" 。另外,为什么要删除 URL 中的百分位数并检查一次。
      猜你喜欢
      • 2019-07-27
      • 2018-05-18
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      相关资源
      最近更新 更多