如何设置此CSS动画复选框的样式?

时间:2017-07-02 23:48:02

标签: css

我有这个复选框,选中后需要这个动画,请看一下视频:https://www.youtube.com/watch?v=lUAccdEOrV8&feature=youtu.be

我在下面尝试了这个,但是我无法让角落变圆,加上我也应用于复选框的不透明度。

#subsDiv input[type="checkbox"]{
  appearance: none;
    background-color: transparent;
    border: 1px solid white;
    padding: 12px;
    border-radius: 6px;
  display: inline-block;
  vertical-align: top;
  margin-left: 0px;
  outline: none;
}

#subsDiv input[type="checkbox"]:hover {
  border-color: #609de6;
}

#subsDiv input[type="checkbox"]:checked {
  outline: none;
  background-position: 0px -24px;
  background-image: url(../img/check.png);
  border-color: #4a90e2;
  animation-name: checkbox;
  animation-duration: 400ms;
}


@keyframes checkbox {
  from{opacity: 1 ;outline: 2px solid #4a90e2; outline-offset: 0px;}
  to{opacity: 0 ;outline: 2px solid #4a90e2; outline-offset: 10px;}
}

1 个答案:

答案 0 :(得分:2)

您可以使用伪元素转换背景,不透明度和transform: scale()来创建在您单击/选中框时爆炸的小边框效果。

.cbl {
  display: block;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: background .5s;
}
.cb {
  display: none;
}
.cb:checked + .cbl {
  background: blue;
}
.cb:checked + .cbl::after {
  content: '\2713';
  color: white;
}
.cbl::before {
  content: '';
  position: absolute;
}
.cbl, .cbl::before {
  width: 1em; height: 1em;
  border-radius: .25em;
  border: 1px solid blue;
}
.cb:checked + .cbl::before {
  transform: scale(2);
  opacity: 0;
  transition: transform .5s, opacity .5s;
}
<input type="checkbox" class="cb" id="cb">
<label class="cbl" for="cb"></label>