【发布时间】:2016-12-12 03:43:41
【问题描述】:
我有以下代码来实现一个切换按钮。这样做的问题是,如果我单击切换按钮外部的左上角或左下角(仍在边界矩形内),则单击或检查操作会像矩形一样被触发。奇怪的是,这只发生在左侧而不是右侧。
如何阻止这种情况?
(要重现问题,请点击切换开关的左上角或左下角)
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 34px;
}
.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: 20px;
width: 20px;
left: 3px;
bottom: 3px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2ECC71;
}
input:focus + .slider {
box-shadow: 0 0 1px #2ECC71;
}
input:checked + .slider:before {
-webkit-transform: translateX(25px);
-ms-transform: translateX(25px);
transform: translateX(25px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
height: 25px;
width: 50px;
}
.slider.round:before {
border-radius: 50%;
}
<label class="switch">
<input type="checkbox">
<div id="utm-parameters-control" class="slider round accordion-control"></div>
</label>
【问题讨论】:
-
因为这行
<label class="switch"></label> -
This answer 可能对你有用。
标签: html css css-selectors pseudo-element