取消选中复选框css3动画

时间:2013-10-30 13:53:25

标签: javascript jquery html css css3

当我勾选复选框时,我有一个动画,但是当你取消选中它时,我想要反向动画。我确实尝试过伪类:未经检查,我发现它没有用。是有css3方法还是小jquery修复?

CODEPEN DEMO

HTML

<div>
  <input type="checkbox" name="cc" id="c1">
  <label for="c1">Checkbox Button 1</label>
</div>

CSS

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,600,700);

@-moz-keyframes checked {
  0% { tranform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}
@-webkit-keyframes checked {
  0% { tranform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}
@keyframes checked {
  0% { tranform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

* {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  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{
    -webkit-appearance: none;
    -moz-appearance: none;
    -ms-appearance: none;
    -o-appearance: none;
    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{
    -webkit-backface-visibility: hidden;
    -moz-backface-visibility: hidden;
    backface-visibility: hidden;
    -webkit-animation: checked 200ms ease 1;
    -moz-animation: checked 200ms ease 1;
    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;
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    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);
}

2 个答案:

答案 0 :(得分:7)

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

:not(:checked)

答案 1 :(得分:1)

我在here中做了一点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);
}
/* ... */