在具有边框半径的 div 之外单击时触发单击

Click triggers when clicked outside the div that have border radius

我有以下代码来实现切换按钮。问题是,如果我单击切换按钮外的左上角或左下角(仍在边界矩形内),则会触发单击或检查操作,就好像它是一个矩形一样。奇怪的是,这只发生在左侧而不是右侧。

这怎么能停止?

( 要重现问题,请单击切换开关的左上角或左下角 )

.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>

您可以从带有 class="switch" 的标签中删除 widthheight。这应该可以解决问题:

.switch {
    position: relative;
    display: inline-block;
    /* width: 40px; */
    /* height: 34px; */

删除 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>

如果您给 label 元素一个 background-color,如下面的代码片段所示,您将看到真正的 "hit area".

.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>

这说明了几个问题:

  1. 你的label比它的children高,
  2. 它也比 children、
  3. 它的 border-radius 与它的 children 不匹配,因为它没有。

要解决这些问题,您需要根据以下代码段对 CSS 进行一些更改(我将 background-color 保留在 label用于可视化目的):

.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>

如下图所示,右侧的 .switch class 宽度不够宽。

screenshot of swift boundary

如果您将 .slider.round 宽度与 .switch 宽度相匹配,那应该可以。我还调整了 .switch class 的高度,使其不超过边界。

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 25px;
}