gpt4 book ai didi

CSS单选按钮 slider 背景颜色

转载 作者:行者123 更新时间:2023-11-28 16:04:52 26 4
gpt4 key购买 nike

您好,我正在尝试将 3 个单选按钮设置为一个 slider ,除背景颜色不会改变外,一切正常。

我用这个例子来处理: https://jsfiddle.net/tx93cpnm/

@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro);
* {
margin: 0px;
padding: 0px;
}
html {
background: #497dd0;
}
.wrapper {
margin: 100px 0;
}
.toggle_radio {
position: relative;
background: rgba(255, 255, 255, .1);
margin: 4px auto;
overflow: hidden;
padding: 0 !important;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
position: relative;
height: 26px;
width: 318px;
}
.toggle_radio > * {
float: left;
}
.toggle_radio input[type=radio] {
display: none;
/*position: fixed;*/
}
.toggle_radio label {
font: 90%/1.618"Source Sans Pro";
color: rgba(255, 255, 255, .9);
z-index: 0;
display: block;
width: 100px;
height: 20px;
margin: 3px 3px;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
cursor: pointer;
z-index: 1;
/*background: rgba(0,0,0,.1);*/
text-align: center;
/*margin: 0 2px;*/
/*background: blue;*/
/*make it blue*/
}
.toggle_option_slider {
/*display: none;*/
/*background: red;*/
width: 100px;
height: 20px;
position: absolute;
top: 3px;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
-webkit-transition: all .4s ease;
-moz-transition: all .4s ease;
-o-transition: all .4s ease;
-ms-transition: all .4s ease;
transition: all .4s ease;
}
#first_toggle:checked ~ .toggle_option_slider {
background: rgba(255, 255, 255, .3);
left: 3px;
}
#second_toggle:checked ~ .toggle_option_slider {
background: rgba(255, 255, 255, .3);
left: 109px;
}
#third_toggle:checked ~ .toggle_option_slider {
background: rgba(255, 255, 255, .3);
left: 215px;
}
<div class="wrapper">
<div class="toggle_radio">
<input type="radio" class="toggle_option" id="first_toggle" name="toggle_option">
<input type="radio" checked class="toggle_option" id="second_toggle" name="toggle_option">
<input type="radio" class="toggle_option" id="third_toggle" name="toggle_option">
<label for="first_toggle">
<p>First Button</p>
</label>
<label for="second_toggle">
<p>Second Button</p>
</label>
<label for="third_toggle">
<p>Third Button</p>
</label>
<div class="toggle_option_slider">
</div>
</div>

然后把它变成这样: https://jsfiddle.net/9us6hs31/

@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro);
* {
margin: 0px;
padding: 0px;
}
html {} .wrapper {
margin: 100px 0;
}
.toggle_radio {
position: relative;
background-color: #ccc;
margin: 4px auto;
overflow: hidden;
padding: 0 !important;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
position: relative;
height: 20px;
width: 60px;
}
.toggle_radio > * {
float: left;
}
.toggle_radio input[type=radio] {
display: none;
/*position: fixed;*/
}
.toggle_radio label {
font: 90%/1.618"Source Sans Pro";
color: white;
z-index: 0;
display: block;
width: 14px;
height: 20px;
margin: 3px 3px;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
cursor: pointer;
z-index: 1;
/*background: rgba(0,0,0,.1);*/
text-align: center;
/*margin: 0 2px;*/
/*background: blue;*/
/*make it blue*/
}
.toggle_option_slider {
/*display: none;*/
/*background: red;*/
content: "";
background-color: white;
width: 14px;
height: 14px;
position: absolute;
top: 3px;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
-webkit-transition: all .4s ease;
-moz-transition: all .4s ease;
-o-transition: all .4s ease;
-ms-transition: all .4s ease;
transition: all .4s ease;
}
#first_toggle:checked ~ .toggle_option_slider {
background-color: #F32121;
left: 4px;
}
#second_toggle:checked ~ .toggle_option_slider {
background-color: white;
left: 23px;
}
#third_toggle:checked ~ .toggle_option_slider {
background-color: #21F321;
left: 42px;
}
#first_toggle:checked ~ .toggle_radio {
background: #F32121;
}
#second_toggle:checked ~ .toggle_radio {
background: #ccc;
}
#third_toggle:checked ~ .toggle_radio {
background: #21F321;
}
<div class="wrapper">
<div class="toggle_radio">
<input type="radio" class="toggle_option" id="first_toggle" name="toggle_option">
<input type="radio" checked class="toggle_option" id="second_toggle" name="toggle_option">
<input type="radio" class="toggle_option" id="third_toggle" name="toggle_option">
<label for="first_toggle"></label>
<label for="second_toggle"></label>
<label for="third_toggle"></label>
<div class="toggle_option_slider">
</div>
</div>

如您所见,我可以更改 slider 的颜色,但不能更改框的颜色。

我周围有这个复选框 slider : https://jsfiddle.net/j1fw0rxv/

.switch {
position: relative;
display: inline-block;
width: 40px;
height: 20px;
}
.switch input {
display: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 14px;
width: 14px;
left: 4px;
bottom: 3px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(18px);
-ms-transform: translateX(18px);
transform: translateX(18px);
}
/* Rounded sliders */

.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<div class="checkbox">
<label class="switch">
<input type="checkbox" value="" id="acheck">
<div class="slider round"></div>
</label>
</div>

所以我试着摆弄那个……

这给了我这样的东西: https://jsfiddle.net/nd18fky6/

.switch {
position: relative;
display: inline-block;
width: 60px;
height: 20px;
}
.switch input {
display: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 14px;
width: 14px;
left: 23px;
bottom: 3px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
#first_toggle:checked + .slider {
background-color: #F32121;
}
#first_toggle:checked + .slider:before {
left: 4px;
}
#second_toggle:checked + .slider {
background-color: #ccc;
}
#second_toggle:checked + .slider:before {
left: 23px;
}
#third_toggle:checked + .slider {
background-color: #21F321;
}
#third_toggle:checked + .slider:before {
left: 42px;
}
/* Rounded sliders */

.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<div class="wrapper">
<div class="switch">
<label class="switch">
</label>
<label for="first_toggle">
<input type="radio" id="first_toggle">
<div class="slider round"></div>
</label>
<label for="second_toggle">
<input type="radio" checked id="second_toggle">
<div class="slider round"></div>
</label>
<label for="third_toggle">
<input type="radio" id="third_toggle">
<div class="slider round"></div>
</label>
</div>
</div>

所以我又做了一些调整,把它们变成了单独的 slider …… https://jsfiddle.net/txz9ceur/

.switch {
position: relative;
display: inline-block;
width: 60px;
height: 20px;
}
.switch input {
display: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 14px;
width: 14px;
left: 23px;
bottom: 3px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
#first_toggle:checked + .slider {
background-color: #F32121;
}
#first_toggle:checked + .slider:before {
left: 4px;
}
#second_toggle:checked + .slider {
background-color: #ccc;
}
#second_toggle:checked + .slider:before {
left: 23px;
}
#third_toggle:checked + .slider {
background-color: #21F321;
}
#third_toggle:checked + .slider:before {
left: 42px;
}
/* Rounded sliders */

.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<div class="wrapper">
<div class="switch">


<label class="switch" for="first_toggle">
<input type="radio" id="first_toggle">
<div class="slider round"></div>
</label>
<label class="switch" for="second_toggle">
<input type="radio" checked id="second_toggle">
<div class="slider round"></div>
</label>
<label class="switch" for="third_toggle">
<input type="radio" id="third_toggle">
<div class="slider round"></div>
</label>

</div>
</div>

好吧,颜色会按应有的方式改变,但我需要一个 slider 。

我错过/忽略了什么(我的猜测是因为方框是方框和选项之间的关系,但我可能是错的......)来获得一个 slider ,其中背景在默认起始位置中间是灰色,向左滑动是红色,向右滑动是绿色?

最佳答案

我玩过一点,但我想我已经记下了。主要问题是您试图用 CSS 影响 parent 的背景……但这是不可能的(目前)。

我使用定位和 z-index 在 slider “track”和一个伪元素(如“button/thumb”)和顶部的标签中分层。

然后通过各种选择器对伪元素应用合适的定位似乎可以解决问题。

@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro);
* {
margin: 0px;
padding: 0px;
}
.wrapper {
margin: 100px 0;
text-align: center;
}
.toggle_radio {
position: relative;
background-color: pink;
margin: 4px auto;
overflow: hidden;
padding: 0 !important;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
position: relative;
height: 28px;
display: inline-block;
}
.toggle_radio input[type=radio] {
display: none;
}
.toggle_radio label {
font: 90%/1.618"Source Sans Pro";
color: white;
z-index: 0;
display: inline-block;
width: 24px;
height: 24px;
margin: 2px;
border-radius: 50%;
cursor: pointer;
z-index: 1;
text-align: center;
background: transparent;
position: relative;
z-index: 3;
}
.toggle_option_slider {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: #ccc;
z-index: 1;
transition: background .35s ease;
}
.toggle_option_slider::after {
content: '';
width: 24px;
height: 24px;
border-radius: 50%;
background: white;
position: absolute;
top: 2px;
z-index: 2;
transition: left .15s ease;
}
#first_toggle:checked ~ .toggle_option_slider {
background: red;
}
#first_toggle:checked ~ .toggle_option_slider::after {
left: 2px;
}
#second_toggle:checked ~ .toggle_option_slider {
background: #ccc;
}
#second_toggle:checked ~ .toggle_option_slider::after {
left: calc((100% - 24px)/2);
}
#third_toggle:checked ~ .toggle_option_slider {
background: green;
}
#third_toggle:checked ~ .toggle_option_slider::after {
left: calc((100% - 24px) - 2px);
}
<div class="wrapper">
<div class="toggle_radio">
<input type="radio" class="toggle_option" id="first_toggle" name="toggle_option">
<input type="radio" checked class="toggle_option" id="second_toggle" name="toggle_option">
<input type="radio" class="toggle_option" id="third_toggle" name="toggle_option">
<label for="first_toggle"></label>
<label for="second_toggle"></label>
<label for="third_toggle"></label>
<div class="toggle_option_slider">
</div>
</div>

关于CSS单选按钮 slider 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39408135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com