尽管类不同,但动画仅在一个复选框上发生

时间:2019-01-21 15:10:08

标签: html css svg

我找到了一个我确实想要使用的复选框,但是当我添加多个复选框时,动画仅适用于第一个。

我尝试创建像.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>

1 个答案:

答案 0 :(得分:2)

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

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

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

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