如何使用伪 CSS 转换更改活动切换按钮的 CSS
How to change the CSS of the active toggle button using pseudo CSS transitions
我的网页上有多个切换按钮。我正在使用 checkbox
输入控件和一些 CSS 来获得 material 切换按钮设计。当我更改任何切换按钮的状态时,CSS 效果会反映在第一个切换按钮上。我怎样才能只在活动切换上应用效果。
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" checked=true class="switch" />
<label for="toggle" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" checked=true class="switch" />
<label for="toggle" class=""></label>
</div>
</div>
这是我正在使用的 CSS。
.material-toggle {
height: 22px;
width: 40px;
margin: 12px;
}
.material-toggle input:empty {
margin-left: -9999px;
}
.material-toggle input:empty ~ label {
position: relative;
float: left;
width: 150px;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.material-toggle input:empty ~ label:before,
.material-toggle input:empty ~ label:after {
position: absolute;
display: block;
content: ' ';
-webkit-transition: all 250ms cubic-bezier(.4,0,.2,1);
transition: all 250ms cubic-bezier(.4,0,.2,1);
}
.material-toggle input:empty ~ label:before {
top: 3px;
left: 0px;
width: 32px;
height: 13px;
border-radius: 12px;
background-color: #bdbdbd;
}
input.switch:empty ~ label:after {
top: -1px;
left: -9px;
width: 1.4em;
height: 8px;
bottom: 0.1em;
margin-left: 0.1em;
background-color: #fff;
border-radius: 50%;
width: 17px;
height: 17px;
border-radius: 50%;
border: solid 2px;
border-color: #fff;
box-shadow: 0 3px 4px 0 rgba(0,0,0,.14),0 3px 3px -2px rgba(0,0,0,.2),0 1px 8px 0 rgba(0,0,0,.12);
}
.material-toggle input:checked ~ label:before {
background-color: #1e88e5;
}
.material-toggle input:checked ~ label:after {
left: 15px;
background-color: #1565c0;
border-color: #1565c0;
}
我附上了 codepen 同样的。
您需要唯一的 ID,目前它们都有 id="toggle"
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" checked=true class="switch" />
<label for="toggle" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle2" name="toggle" checked=true class="switch" />
<label for="toggle2" class=""></label>
</div>
</div>
如果你想有多个切换按钮,你需要使用ID,但是你在两个切换中有相同的ID,这是不可能发生的,因为ID是唯一的,所以改变ID和label for
以定位这些 ID。
.material-toggle {
height: 22px;
width: 40px;
margin: 12px;
}
.material-toggle input:empty {
margin-left: -9999px;
}
.material-toggle input:empty ~ label {
position: relative;
float: left;
width: 150px;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.material-toggle input:empty ~ label:before,
.material-toggle input:empty ~ label:after {
position: absolute;
display: block;
content: ' ';
-webkit-transition: all 250ms cubic-bezier(.4, 0, .2, 1);
transition: all 250ms cubic-bezier(.4, 0, .2, 1);
}
.material-toggle input:empty ~ label:before {
top: 3px;
left: 0px;
width: 32px;
height: 13px;
border-radius: 12px;
background-color: #bdbdbd;
}
input.switch:empty ~ label:after {
top: -1px;
left: -9px;
width: 1.4em;
height: 8px;
bottom: 0.1em;
margin-left: 0.1em;
background-color: #fff;
border-radius: 50%;
width: 17px;
height: 17px;
border-radius: 50%;
border: solid 2px;
border-color: #fff;
box-shadow: 0 3px 4px 0 rgba(0, 0, 0, .14), 0 3px 3px -2px rgba(0, 0, 0, .2), 0 1px 8px 0 rgba(0, 0, 0, .12);
}
.material-toggle input:checked ~ label:before {
background-color: #1e88e5;
}
.material-toggle input:checked ~ label:after {
left: 15px;
background-color: #1565c0;
border-color: #1565c0;
}
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" checked=true class="switch" />
<label for="toggle" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle2" name="toggle" checked=true class="switch" />
<label for="toggle2" class=""></label>
</div>
</div>
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" class="switch" />
<label for="toggle" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle-2" name="toggle" class="switch" />
<label for="toggle-2" class=""></label>
</div>
</div>
我解决了为 值分配不同标签的 。我做了一个 material-主题切换器,但我使用了 Javascript 和 Jquery 来让它在一个页面中使用多个切换器。
你为什么不使用不同的 "id"。
如果我理解这个问题,答案是:
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle1" name="toggle" checked=true class="switch" />
<label for="toggle1" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle2" name="toggle" checked=false class="switch" />
<label for="toggle2" class=""></label>
</div>
</div>
我的网页上有多个切换按钮。我正在使用 checkbox
输入控件和一些 CSS 来获得 material 切换按钮设计。当我更改任何切换按钮的状态时,CSS 效果会反映在第一个切换按钮上。我怎样才能只在活动切换上应用效果。
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" checked=true class="switch" />
<label for="toggle" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" checked=true class="switch" />
<label for="toggle" class=""></label>
</div>
</div>
这是我正在使用的 CSS。
.material-toggle {
height: 22px;
width: 40px;
margin: 12px;
}
.material-toggle input:empty {
margin-left: -9999px;
}
.material-toggle input:empty ~ label {
position: relative;
float: left;
width: 150px;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.material-toggle input:empty ~ label:before,
.material-toggle input:empty ~ label:after {
position: absolute;
display: block;
content: ' ';
-webkit-transition: all 250ms cubic-bezier(.4,0,.2,1);
transition: all 250ms cubic-bezier(.4,0,.2,1);
}
.material-toggle input:empty ~ label:before {
top: 3px;
left: 0px;
width: 32px;
height: 13px;
border-radius: 12px;
background-color: #bdbdbd;
}
input.switch:empty ~ label:after {
top: -1px;
left: -9px;
width: 1.4em;
height: 8px;
bottom: 0.1em;
margin-left: 0.1em;
background-color: #fff;
border-radius: 50%;
width: 17px;
height: 17px;
border-radius: 50%;
border: solid 2px;
border-color: #fff;
box-shadow: 0 3px 4px 0 rgba(0,0,0,.14),0 3px 3px -2px rgba(0,0,0,.2),0 1px 8px 0 rgba(0,0,0,.12);
}
.material-toggle input:checked ~ label:before {
background-color: #1e88e5;
}
.material-toggle input:checked ~ label:after {
left: 15px;
background-color: #1565c0;
border-color: #1565c0;
}
我附上了 codepen 同样的。
您需要唯一的 ID,目前它们都有 id="toggle"
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" checked=true class="switch" />
<label for="toggle" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle2" name="toggle" checked=true class="switch" />
<label for="toggle2" class=""></label>
</div>
</div>
如果你想有多个切换按钮,你需要使用ID,但是你在两个切换中有相同的ID,这是不可能发生的,因为ID是唯一的,所以改变ID和label for
以定位这些 ID。
.material-toggle {
height: 22px;
width: 40px;
margin: 12px;
}
.material-toggle input:empty {
margin-left: -9999px;
}
.material-toggle input:empty ~ label {
position: relative;
float: left;
width: 150px;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.material-toggle input:empty ~ label:before,
.material-toggle input:empty ~ label:after {
position: absolute;
display: block;
content: ' ';
-webkit-transition: all 250ms cubic-bezier(.4, 0, .2, 1);
transition: all 250ms cubic-bezier(.4, 0, .2, 1);
}
.material-toggle input:empty ~ label:before {
top: 3px;
left: 0px;
width: 32px;
height: 13px;
border-radius: 12px;
background-color: #bdbdbd;
}
input.switch:empty ~ label:after {
top: -1px;
left: -9px;
width: 1.4em;
height: 8px;
bottom: 0.1em;
margin-left: 0.1em;
background-color: #fff;
border-radius: 50%;
width: 17px;
height: 17px;
border-radius: 50%;
border: solid 2px;
border-color: #fff;
box-shadow: 0 3px 4px 0 rgba(0, 0, 0, .14), 0 3px 3px -2px rgba(0, 0, 0, .2), 0 1px 8px 0 rgba(0, 0, 0, .12);
}
.material-toggle input:checked ~ label:before {
background-color: #1e88e5;
}
.material-toggle input:checked ~ label:after {
left: 15px;
background-color: #1565c0;
border-color: #1565c0;
}
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" checked=true class="switch" />
<label for="toggle" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle2" name="toggle" checked=true class="switch" />
<label for="toggle2" class=""></label>
</div>
</div>
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle" name="toggle" class="switch" />
<label for="toggle" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle-2" name="toggle" class="switch" />
<label for="toggle-2" class=""></label>
</div>
</div>
我解决了为 值分配不同标签的 。我做了一个 material-主题切换器,但我使用了 Javascript 和 Jquery 来让它在一个页面中使用多个切换器。
你为什么不使用不同的 "id"。 如果我理解这个问题,答案是:
<div>
<div class="material-toggle">
<input type="checkbox" id="toggle1" name="toggle" checked=true class="switch" />
<label for="toggle1" class=""></label>
</div>
<div class="material-toggle">
<input type="checkbox" id="toggle2" name="toggle" checked=false class="switch" />
<label for="toggle2" class=""></label>
</div>
</div>