【问题标题】:Click triggers when clicked outside the div that have border radius在具有边框半径的 div 外部单击时单击触发器
【发布时间】: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>

【问题讨论】:

  • 因为这行&lt;label class="switch"&gt;&lt;/label&gt;
  • This answer 可能对你有用。

标签: html css css-selectors pseudo-element


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果你给你的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>

      这说明了几个问题:

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

      要解决这些问题,您需要根据以下代码段对 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>

      【讨论】:

        【解决方案4】:

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

        screenshot of swift boundary

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

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

        【讨论】:

          猜你喜欢
          • 2015-09-21
          • 1970-01-01
          • 2018-02-08
          • 1970-01-01
          • 1970-01-01
          • 2013-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多