单击复选框有时会切换其他复选框

时间:2019-03-26 13:07:30

标签: html css checkbox

所以我得到了5 ╦═════════════════════╦═════════════════════════╗ ║ Falsy ║ Truthy ║ ╠═════════════════════╬═════════════════════════╣ ║ 0 ║ Non-0 ║ ║ @() ║ @(1) ║ ║ @(0) ║ @(0, 1) ║ ║ [string]::Empty ║ Non-empty string ║ ║ $false ║ $true ║ ║ 0-backed enum value ║ Any non-0 enum value ║ ║ $null ║ Any non-$null reference ║ ╚═════════════════════╩═════════════════════════╝ 。所有下面一个。它们是使用checkboxsvg制作的。当我单击每个复选框的标签时,它会切换正确的复选框,但是当我单击复选框的图像时,有时它会切换不正确的复选框!

polyline

html

.cbx {
  margin: auto;
  margin-top: 2px;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
}
.cbx span {
  display: inline-block;
  vertical-align: middle;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
  position: relative;
  width: 18px;
  height: 18px;
  border-radius: 3px;
  transform: scale(1);
  vertical-align: middle;
  border: 1px solid #9098A9;
  transition: all 0.2s ease;
}
.cbx span:first-child svg {
  position: absolute;
  top: 3px;
  left: 2px;
  fill: none;
  stroke: #FFFFFF;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 16px;
  stroke-dashoffset: 16px;
  transition: all 0.3s ease;
  transition-delay: 0.1s;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child:before {
  content: "";
  width: 100%;
  height: 100%;
  background: #506EEC;
  display: block;
  transform: scale(0);
  opacity: 1;
  border-radius: 50%;
}
.cbx span:last-child {
  padding-left: 8px;
}
.cbx:hover span:first-child {
  border-color: #506EEC;
}

.inp-cbx:checked + .cbx span:first-child {
  background: #506EEC;
  border-color: #506EEC;
  animation: wave 0.4s ease;
}
.inp-cbx:checked + .cbx span:first-child svg {
  stroke-dashoffset: 0;
}
.inp-cbx:checked + .cbx span:first-child:before {
  transform: scale(3.5);
  opacity: 0;
  transition: all 0.6s ease;
}

@keyframes wave {
  50% {
    transform: scale(0.9);
  }
}

选择复选框1、2、3、4。然后单击复选框3取消选中它。复选框4变为未选中状态。再次单击复选框3时,将其取消选中。

链接到codepen

5 个答案:

答案 0 :(得分:1)

因为transform:在选中时scale(3.5)会扩大其大小并覆盖其他复选框,这就是为什么。

.inp-cbx:checked + .cbx span:first-child:before {
  transform: scale(1); // set scale to it's actual size instead of 3.5
  opacity: 0;
  transition: all 0.6s ease;
}

.cbx {
  margin: auto;
  margin-top: 2px;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
}
.cbx span {
  display: inline-block;
  vertical-align: middle;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
  position: relative;
  width: 18px;
  height: 18px;
  border-radius: 3px;
  transform: scale(1);
  vertical-align: middle;
  border: 1px solid #9098A9;
  transition: all 0.2s ease;
}
.cbx span:first-child svg {
  position: absolute;
  top: 3px;
  left: 2px;
  fill: none;
  stroke: #FFFFFF;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 16px;
  stroke-dashoffset: 16px;
  transition: all 0.3s ease;
  transition-delay: 0.1s;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child:before {
  content: "";
  width: 100%;
  height: 100%;
  background: #506EEC;
  display: block;
  transform: scale(0);
  opacity: 1;
  border-radius: 50%;
}
.cbx span:last-child {
  padding-left: 8px;
}
.cbx:hover span:first-child {
  border-color: #506EEC;
}

.inp-cbx:checked + .cbx span:first-child {
  background: #506EEC;
  border-color: #506EEC;
  animation: wave 0.4s ease;
}
.inp-cbx:checked + .cbx span:first-child svg {
  stroke-dashoffset: 0;
}
.inp-cbx:checked + .cbx span:first-child:before {
  transform: scale(1);
  opacity: 0;
  transition: all 0.6s ease;
}

@keyframes wave {
  50% {
    transform: scale(0.9);
  }
}
<div class="ax-cb-div">
  <input class="inp-cbx" id="1" type="checkbox" style="display: none;"/>
  <label for="1" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 1</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="2" type="checkbox" style="display: none;"/>
  <label for="2" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 2</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="3" type="checkbox" style="display: none;"/>
  <label for="3" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 3</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="4" type="checkbox" style="display: none;"/>
  <label for="4" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 4</span>
  </label>
</div>

答案 1 :(得分:1)

将指针事件都不添加到跨度似乎是可行的解决方法

.cbx span {
  pointer-events: none;
}

https://codepen.io/chemicalbr/pen/KEYbgG

答案 2 :(得分:0)

您可以使用值标签。

<input id="1" value="test" ../>
<input id="3" value="test" ../>

MDN Web docs - Input

答案 3 :(得分:0)

将变换属性值更改为1.5

.inp-cbx:checked + .cbx span:first-child:before {
   transform: scale(1.5);
   opacity: 0;
   transition: all 0.6s ease;
}

答案 4 :(得分:0)

pointer-events: none;添加到.cbx span:first-child:before样式中。导致问题的是动画。

.cbx span:first-child:before中,我们将content:""scale(0)opacity:1分别缩放到scale(3.5)opacity:0。因此,实际上我们只是以不透明的方式隐藏该对象,这意味着该对象将在那里并且可以单击。这就是为什么我们需要在pointer-events:none样式中添加.cbx span:first-child:before

要对其进行测试,只需选中第4个复选框并以.inp-cbx:checked + .cbx span:first-child:before样式进行检查。将不透明度从0更改为opacity:1。您将看到类似下面的内容。

enter image description here

在上图中,您可以看到第四个复选框的span:before覆盖了第三个复选框。它是可点击的。这就是为什么当您单击第三个复选框时,它实际上会单击第四个复选框。

请参见下面的代码段。

.cbx {
  margin: auto;
  margin-top: 2px;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
}
.cbx span {
  display: inline-block;
  vertical-align: middle;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
  position: relative;
  width: 18px;
  height: 18px;
  border-radius: 3px;
  transform: scale(1);
  vertical-align: middle;
  border: 1px solid #9098A9;
  transition: all 0.2s ease;
}
.cbx span:first-child svg {
  position: absolute;
  top: 3px;
  left: 2px;
  fill: none;
  stroke: #FFFFFF;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 16px;
  stroke-dashoffset: 16px;
  transition: all 0.3s ease;
  transition-delay: 0.1s;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child:before {
  content: "";
  width: 100%;
  height: 100%;
  background: #506EEC;
  display: block;
  transform: scale(0);
  opacity: 1;
  border-radius: 50%;
  pointer-events:none;
}
.cbx span:last-child {
  padding-left: 8px;
}
.cbx:hover span:first-child {
  border-color: #506EEC;
}

.inp-cbx:checked + .cbx span:first-child {
  background: #506EEC;
  border-color: #506EEC;
  animation: wave 0.4s ease;
}
.inp-cbx:checked + .cbx span:first-child svg {
  stroke-dashoffset: 0;
}
.inp-cbx:checked + .cbx span:first-child:before {
  transform: scale(3.5);
  opacity: 0;
  transition: all 0.6s ease;
}

@keyframes wave {
  50% {
    transform: scale(0.9);
  }
}
<div class="ax-cb-div">
  <input class="inp-cbx" id="1" type="checkbox" style="display: none;"/>
  <label for="1" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 1</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="2" type="checkbox" style="display: none;"/>
  <label for="2" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 2</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="3" type="checkbox" style="display: none;"/>
  <label for="3" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 3</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="4" type="checkbox" style="display: none;"/>
  <label for="4" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 4</span>
  </label>
</div>