2

当我的复选框被选中时,我有一个动画,但我想在它们未被选中时反转动画。我尝试了伪类:unchecked,我发现它不起作用。是否有 CSS3 方法或一个小的 jQuery 修复?

@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,600,700);
@keyframes checked {
  0% {
    tranform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

* {
  box-sizing: border-box;
}

body {
  color: #fff;
  font-family: "Open Sans";
  background: #DA5838;
  padding: 20px;
}

h2 {
  font-size: 24px;
  margin-bottom: 14px;
}

div {
  margin-bottom: 18px;
}

input[type="radio"],
input[type="checkbox"] {
  display: none;
}

input[type="radio"]+label,
input[type="checkbox"]+label {
  appearance: none;
  cursor: pointer;
  display: inline-block;
  padding-left: 34px;
  position: relative;
  vertical-align: middle;
}

input[type="radio"]:checked+label,
input[type="checkbox"]:checked+label {
  backface-visibility: hidden;
  animation: checked 200ms ease 1;
}

input[type="radio"]+label:before {
  background: none repeat scroll 0 0 rgba(255, 255, 255, 0);
  border-radius: 100% 100% 100% 100%;
  content: " ";
  height: 7px;
  left: 12px;
  position: absolute;
  top: 6px;
  width: 7px;
}

input[type="radio"]+label:hover:before {
  background: none repeat scroll 0 0 rgba(255, 255, 255, 0.5);
}

input[type="radio"]:checked+label:before {
  background: none repeat scroll 0 0 rgba(255, 255, 255, 1);
}

input[type="checkbox"]+label:before {
  content: '';
  position: absolute;
  width: 7px;
  height: 3px;
  top: 6px;
  left: 11px;
  border: 2px solid rgba(255, 255, 255, 0);
  border-top: none;
  border-right: none;
  transform: rotate(-45deg);
}

input[type="checkbox"]+label:hover:before {
  border-color: rgba(255, 255, 255, 0.5);
}

input[type="checkbox"]:checked+label:before {
  border-color: rgba(255, 255, 255, 1);
}

input[type="radio"]+label:after,
input[type="checkbox"]+label:after {
  border: 3px solid #FFFFFF;
  border-radius: 100% 100% 100% 100%;
  content: " ";
  height: 15px;
  left: 5px;
  position: absolute;
  top: -1px;
  width: 15px;
}

input[type="radio"]:checked+label:after,
input[type="checkbox"]:checked+label:after {
  border-color: rgba(255, 255, 255, 1);
}
<div>
  <input type="radio" name="rr" id="r1">
  <label for="r1">Radio Button 1</label>
</div>
<div>
  <input type="radio" name="rr" id="r2">
  <label for="r2">Radio Button 2</label>
</div>
<div>
  <input type="radio" name="rr" id="r3">
  <label for="r3">Radio Button 3</label>
</div>
<div>
  <input type="radio" name="rr" id="r4">
  <label for="r4">Radio Button 4</label>
</div>
<br/><br/>
<div>
  <input type="checkbox" name="cc" id="c1">
  <label for="c1">Checkbox Button 1</label>
</div>
<div>
  <input type="checkbox" name="cc" id="c2">
  <label for="c2">Checkbox Button 2</label>
</div>
<div>
  <input type="checkbox" name="cc" id="c3">
  <label for="c3">Checkbox Button 3</label>
</div>
<div>
  <input type="checkbox" name="cc" id="c4">
  <label for="c4">Checkbox Button 4</label>
</div>

CodePen 演示

4

2 回答 2

7

简单的解决方案是最好的!

:not(:checked)
于 2013-10-30T14:03:31.887 回答
0

我在您的CODEPEN DEMO中进行了一些更改,现在支持使用键盘的可访问性。

/* ... */
input[type="radio"],
input[type="checkbox"]{
    opacity: 0;
    position: absoute;
}
/* ... */
input[type="radio"]:focus + label:before,
input[type="radio"] + label:hover:before {
    background: none repeat scroll 0 0 rgba(255, 255, 255, 0.5);
}
/* ... */
input[type="checkbox"]:focus + label:before,
input[type="checkbox"] + label:hover:before {
    border-color: rgba(255, 255, 255, 0.5);
}
/* ... */

于 2015-10-05T19:43:35.013 回答