我有以下代码来实现切换按钮。这样做的问题是,如果我点击切换按钮外部的左上角或左下角(仍然在边界矩形内),点击或检查操作将被触发,就像它是一个矩形一样。奇怪的是,这只发生在左侧,而不是右侧。
如何停止?
(要重新创建问题,请单击切换开关的左上角或左下角)
.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>
&#13;
答案 0 :(得分:2)
您可以使用class="switch"
从标签中删除宽度和高度。这应该可以解决问题:
.switch {
position: relative;
display: inline-block;
/* width: 40px; */
/* height: 34px; */
答案 1 :(得分:1)
删除label
的高度和宽度,然后你去!
.switch {
position: relative;
display: inline-block;
}
.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>
答案 2 :(得分:1)
如果您将label
元素设为background-color
,如下面的代码段所示,您将看到真正的“点击区域”。
.switch {
background:red;
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
比其子女高,border-radius
与其子项不匹配,因为它没有。{/ li>
醇>
要解决这些问题,您需要对CSS进行一些更改,例如以下代码段(我已将background-color
保留在label
上以进行可视化目的):< / p>
.switch {
background:red;
border-radius:25px;
position: relative;
display: inline-block;
width: 50px;
height: 25px;
}
.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: 25px;
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>
答案 3 :(得分:0)
如下图所示,.switch类宽度在右侧不够宽。
如果你将.slider.round宽度与.switch宽度匹配应该有效。我还调整了.switch类的高度,使其边界不超过。
.switch {
position: relative;
display: inline-block;
width: 50px;
height: 25px;
}