为什么自定义 CSS 样式由于垂直滑块而不适用?
Why does custom CSS styling not apply due to vertical slider?
#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。如何让它发挥作用?谢谢!
为了向范围滑块添加自定义样式,您需要设置 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>
然后你会发现垂直滑块
似乎没有可用于设计垂直范围的解决方法,您可以做的是旋转默认值。
.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
编辑:根据您的评论,我已将滑块包裹到 flex 容器中以对齐它们。
/* 你能试试下面的 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;
}
}
#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" />
#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。如何让它发挥作用?谢谢!
为了向范围滑块添加自定义样式,您需要设置 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>
然后你会发现垂直滑块
似乎没有可用于设计垂直范围的解决方法,您可以做的是旋转默认值。
.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
编辑:根据您的评论,我已将滑块包裹到 flex 容器中以对齐它们。
/* 你能试试下面的 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;
}
}
#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" />