【问题标题】:Why does custom CSS styling not apply due to vertical slider?为什么由于垂直滑块而自定义 CSS 样式不适用?
【发布时间】:2023-03-15 02:48:01
【问题描述】:

#slider1 {
  -webkit-appearance: slider-vertical; /* This makes ... */
  background-color: black;
  width: 1px;
  height: 100px;
}

#slider2 {
  -webkit-appearance: none; /* the difference. */
  background-color: black;
  width: 100px;
  height: 1px;
}

#slider1::-webkit-slider-thumb,
#slider2::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  background: #04aa6d;
  cursor: pointer;
}

#slider1::-webkit-slider-thumb:hover,
#slider2::-webkit-slider-thumb:hover {
  width: 20px;
  height: 20px;
}
<input type="range" id="slider1" />
<input type="range" id="slider2" />

如图所示,两个滑块的 CSS 样式几乎相同,除了 -webkit-appearance 属性。但是水平滑块(这是默认滑块)接受样式,而垂直滑块拒绝它。我在 Chrome 上。如何让它发挥作用?谢谢!

【问题讨论】:

标签: html css styling


【解决方案1】:

似乎没有可用于设计垂直范围的解决方法,您可以做的是旋转默认范围。

.wrap {
  display: flex;
}

.slid1 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  width: 30px;
}

#slider1,
#slider2 {
  -webkit-appearance: none;
  /* the difference. */
  background-color: black;
  width: 100px;
  height: 1px;
}

#slider1 {
  -webkit-appearance: none;
  /* This makes ... */
  transform: rotate(270deg);
  transform-origin: center;
}

#slider1::-webkit-slider-thumb,
#slider2::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  background: #04aa6d;
  cursor: pointer;
}

#slider1::-webkit-slider-thumb:hover,
#slider2::-webkit-slider-thumb:hover {
  width: 20px;
  height: 20px;
}
<div class="wrap">
  <div class="slid1">
    <input type="range" id="slider1" />
  </div>
  <div class="slid2">
    <input type="range" id="slider2" />
  </div>
</div>

Source

编辑:根据您的评论,我已将滑块包装到弹性容器中以对齐它们。

【讨论】:

  • 是的,我尝试使用该解决方案,但正如您所见,代码产生了一个向右移动的滑块。你有解决方案吗?我的意思是一个通用的解决方案,而不是这个。
  • @BookOfFlames 我已经更新了我的答案,希望对您有所帮助
【解决方案2】:

为了向范围滑块添加自定义样式,您需要设置一个 CSS 属性 -webkit-appearance: none;,您在 #slider2 中执行此操作,但在 #slider1 中错过了它. 试试下面的代码

<input type="range" id="slider1" />
<input type="range" id="slider2" />
<style type="text/css">
    #slider1 {
    -webkit-appearance: none; /* This makes ... */
    background-color: black;
    width: 1px;
    height: 100px;
    }
    
    #slider2 {
    -webkit-appearance: none; /* the difference. */
    background-color: black;
    width: 100px;
    height: 1px;
    }
    
    #slider1::-webkit-slider-thumb,
    #slider2::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 25px;
    height: 25px;
    background: #04aa6d;
    cursor: pointer;
    }
    
    #slider1::-webkit-slider-thumb:hover,
    #slider2::-webkit-slider-thumb:hover {
    width: 20px;
    height: 20px;
    }
</style>

然后你会找到垂直滑块

【讨论】:

  • 您的代码生成的滑块似乎滑动不顺畅,它只是看起来垂直。
【解决方案3】:

/* 你可以试试下面的 CSS 代码和你的 CSS */

/*Chrome*/
@media screen and (-webkit-min-device-pixel-ratio:0) {
    input[type='range'] {
      overflow: hidden;
      width: 80px;
      -webkit-appearance: none;      
    }    
    input[type='range']::-webkit-slider-runnable-track {
      height: 10px;
      -webkit-appearance: none;
      color: #13bba4;
      margin-top: -1px;
    }
       
}

【讨论】:

    【解决方案4】:

    #slider1 {
      margin-top: 50px;
      transform: rotate(90deg);
    }
    
    #slider1, #slider2 {
      -webkit-appearance: none; /* the difference. */
      background-color: black;
      width: 100px;
      height: 1px;
    }
    
    #slider1::-webkit-slider-thumb,
    #slider2::-webkit-slider-thumb {
      -webkit-appearance: none;
      appearance: none;
      width: 25px;
      height: 25px;
      background: #04aa6d;
      cursor: pointer;
    }
    
    #slider1::-webkit-slider-thumb:hover,
    #slider2::-webkit-slider-thumb:hover {
      width: 20px;
      height: 20px;
    }
    <input type="range" id="slider1" />
    <input type="range" id="slider2" />

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多