Я не могу изменить фон своего div, когда проверяется ввод. Когда я проверяю второй ввод, фон div должен измениться, возможно, с анимацией слева направо. Кто-нибудь может мне помочь? Это HTML:
.first_switcher {
display: block;
margin: 0 auto;
height: 38px;
margin-top: 50px;
padding: 0px;
background: dimgray;
width: 200px;
border-radius: 38px;
position: relative;
}
.first_switcher__input2 {
display: none;
}
.first_switcher__input1 {
display: none;
}
.first_switcher__label1 {
float: left;
width: 100px;
font-size: 12px;
line-height: 38px;
color: #ffffff;
text-align: center;
cursor: pointer;
z-index: 10;
}
.first_switcher__label2 {
float: left;
width: 100px;
font-size: 12px;
line-height: 38px;
color: dimgray;
text-align: center;
cursor: pointer;
z-index: 10;
}
.first_switcher__input1:checked+.first_switcher__label1 {
color: white;
}
.first_switcher__input2:checked+.first_switcher__label2 {
color: white;
}
.first_switcher__input1:not(:checked)+.first_switcher__label1 {
color: dimgray;
}
.first_switcher__input2:checked+.first_switcher {
background: black;
}<div class = "first_switcher">
<input type = "radio" name = "balance" id = "on" class = "first_switcher__input1" checked/>
<label for = "on" class = "first_switcher__label1">ON</label>
<input type = "radio" name = "balance" id = "off" class = "first_switcher__input2"/>
<label for = "off" class = "first_switcher__label2">OFF</label>
</div>вы можете сделать это с помощью js
Тем не менее, вы можете добиться визуального эффекта, если разместите дополнительный элемент span в конце контейнера, который затем вы позиционируете абсолютно так, чтобы он покрыл весь контейнер (потребуется некоторый z-индекс, чтобы промежуток находился «между ”Фон и входы), а затем измените фон на основе проверенного состояния поля ввода.






Вы не можете настроить таргетинг на родителя через CSS, вам нужно использовать JS. Но в селекторе братьев и сестер должны быть пробелы между знаком «+», например:
.first_switcher__input1:checked + .first_switcher__label1 {
color: white;
}
.first_switcher__input2:checked + .first_switcher__label2 {
color: white;
}
.first_switcher__input1:not(:checked) + .first_switcher__label1 {
color: dimgray;
}
Используйте JS для этого onchange установите background как родительский (оберните div) и измените css, как показано ниже.
function changeOn(ele){
if (ele.checked)
{
document.getElementsByClassName("first_switcher")[0].style.background = "red";
document.getElementsByClassName("first_switcher__label2")[0].style.color = "transparent"
}
}
function changeOff(ele){
if (ele.checked)
{
document.getElementsByClassName("first_switcher")[0].style.background = "dimgray";
document.getElementsByClassName("first_switcher__label2")[0].style.color = "white"
}
} .first_switcher {
display: block;
margin: 0 auto;
height: 38px;
margin-top: 50px;
padding: 0px;
background: red;
width: 200px;
border-radius: 38px;
position: relative;
}
.first_switcher__input2 {
display: none;
}
.first_switcher__input1 {
display: none;
}
.first_switcher__label1 {
float: left;
width: 100px;
font-size: 12px;
line-height: 38px;
color: #ffffff;
text-align: center;
cursor: pointer;
z-index: 10;
}
.first_switcher__label2 {
float: left;
width: 100px;
font-size: 12px;
line-height: 38px;
color: transparent;
text-align: center;
cursor: pointer;
z-index: 10;
}
.first_switcher__input1:checked+.first_switcher__label1 {
color: white;
}
.first_switcher__input2:checked+.first_switcher__label2 {
color: white;
}
.first_switcher__input1:not(:checked)+.first_switcher__label1 {
color: dimgray;
}
.first_switcher__input2:checked+.first_switcher {
background: black;
} <div class = "first_switcher">
<input type = "radio" name = "balance" id = "on" class = "first_switcher__input1" onchange = "changeOn(this)" checked/>
<label for = "on" class = "first_switcher__label1">ON</label>
<input onchange = "changeOff(this)" type = "radio" name = "balance" id = "off" class = "first_switcher__input2"/>
<label for = "off" class = "first_switcher__label2">OFF</label>
</div>Если вы хотите использовать css, рассмотрите приведенный ниже фрагмент. изменить цвет в соответствии с вашими потребностями.
.first_switcher {
display: block;
margin: 0 auto;
height: 38px;
margin-top: 50px;
padding: 0px;
background: dimgray;
width: 200px;
border-radius: 38px;
position: relative;
}
.first_switcher__input2 {
display: none;
}
.first_switcher__input1 {
display: none;
}
.first_switcher__label1 {
float: left;
width: 100px;
font-size: 12px;
line-height: 38px;
color: #ffffff;
text-align: center;
cursor: pointer;
z-index: 10;
}
.first_switcher__label2 {
float: left;
width: 100px;
font-size: 12px;
line-height: 38px;
color: dimgray;
text-align: center;
cursor: pointer;
z-index: 10;
}
.first_switcher__input1:checked+.first_switcher__label1 {
color: white;
}
.first_switcher__input2:checked+.first_switcher__label2 {
color: white;
}
.first_switcher__input1:not(:checked)+.first_switcher__label1 {
color: dimgray;
}
.first_switcher__input2:checked+.first_switcher {
background: black;
}
/*.first_switcher__input1:checked+.first_switcher__label1 {
background: red;
}
.first_switcher__input2:not(:checked)+.first_switcher__label2 {
background: red;
} */
.first_switcher__input1:not(:checked)+.first_switcher__label1 {
color:blue;
background: blue;
border-top-left-radius: 19px;
border-bottom-left-radius: 19px;
}
.first_switcher__input2:checked+.first_switcher__label2 {
background: blue;
border-top-right-radius: 19px;
border-bottom-right-radius: 19px;
}<div class = "first_switcher">
<input type = "radio" name = "balance" id = "on" class = "first_switcher__input1" checked/>
<label for = "on" class = "first_switcher__label1">ON</label>
<input type = "radio" name = "balance" id = "off" class = "first_switcher__input2"/>
<label for = "off" class = "first_switcher__label2">OFF</label>
</div>
вы не можете стилизовать родителя в соответствии с дочерним элементом в css