0

我找到了一个我真的想使用的复选框,但是当我添加超过 1 个复选框时,动画仅适用于第一个。

我已经尝试创建新的类.cbx2.inp-cbx2并应用相同的样式,但动画仍然只适用于第一个复选框。

你能解释一下我哪里出错了吗?

谢谢。

/* #### CHECKBOX STYLES AND ANIMATION #### */
.cbx {
  margin: auto;
  -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: 50px;
  height: 50px;
  border-radius: 0px;
  transform: scale(1);
  vertical-align: middle;
  border: 1px solid #9098A9;
  transition: all 0.2s ease;
}
.cbx span:first-child svg {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  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: #2bbfcb;
  display: block;
  transform: scale(0);
  opacity: 1;
  border-radius: 0%;
}
.cbx span:last-child {
  padding-left: 8px;
}
.cbx:hover span:first-child {
  border-color: #2bbfcb;
}

.inp-cbx:checked + .cbx span:first-child {
  background: #2bbfcb;
  border-color: #2bbfcb;
  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);
  }
}



/* #### CHECKBOX STYLES AND ANIMATION #### */
.cbx2 {
  margin: auto;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
}
.cbx2 span {
  display: inline-block;
  vertical-align: middle;
  transform: translate3d(0, 0, 0);
}
.cbx2 span:first-child {
  position: relative;
  width: 50px;
  height: 50px;
  border-radius: 0px;
  transform: scale(1);
  vertical-align: middle;
  border: 1px solid #9098A9;
  transition: all 0.2s ease;
}
.cbx2 span:first-child svg {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  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);
}
.cbx2 span:first-child:before {
  content: "";
  width: 100%;
  height: 100%;
  background: #2bbfcb;
  display: block;
  transform: scale(0);
  opacity: 1;
  border-radius: 0%;
}
.cbx2 span:last-child {
  padding-left: 8px;
}
.cbx2:hover span:first-child {
  border-color: #2bbfcb;
}

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

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


          <input class="inp-cbx2" id="cbx2" type="checkbox" style="display: none;"/>
          <label class="cbx2" for="cbx"><span>
          <svg width="40px" height="40px" viewbox="0 0 12 10" class="c">
            <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
          </svg></span><span>MUTI ROUTE</span></label>

4

1 回答 1

2

CSS 中存在类的全部原因是让您的样式可跨多个元素重用。无需复制您的 CSS。

使用与第一个相同的类时,第二个复选框不起作用的原因是您尚未更新第二个标签for属性。此属性告诉浏览器id单击标签时应影响的表单元素。

在您的情况下,第二个标签应如下所示:

<label class="cbx" for="cbx2"><span>

/* #### CHECKBOX STYLES AND ANIMATION #### */

.cbx {
  margin: auto;
  -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: 50px;
  height: 50px;
  border-radius: 0px;
  transform: scale(1);
  vertical-align: middle;
  border: 1px solid #9098A9;
  transition: all 0.2s ease;
}

.cbx span:first-child svg {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  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: #2bbfcb;
  display: block;
  transform: scale(0);
  opacity: 1;
  border-radius: 0%;
}

.cbx span:last-child {
  padding-left: 8px;
}

.cbx:hover span:first-child {
  border-color: #2bbfcb;
}

.inp-cbx:checked+.cbx span:first-child {
  background: #2bbfcb;
  border-color: #2bbfcb;
  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);
  }
}
<input class="inp-cbx" id="cbx" type="checkbox" style="display: none;" />
<label class="cbx" for="cbx"><span>
<svg width="40px" height="40px" viewbox="0 0 12 10">
    <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
</svg></span><span>DIRECT</span></label>


<input class="inp-cbx" id="cbx2" type="checkbox" style="display: none;" />
<label class="cbx" for="cbx2"><span>
<svg width="40px" height="40px" viewbox="0 0 12 10" class="c">
    <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
</svg></span><span>MUTI ROUTE</span></label>

于 2019-01-21T15:15:19.450 回答