2ne
2ne

Reputation: 5986

Uncheck checkbox css3 animation

I have an animation when my checkboxes are checked, but I want to reverse the animation when they are unchecked. I tried the pseudo class :unchecked, which I found did not work. Is there a CSS3 approach or maybe a small jQuery fix?

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

Upvotes: 2

Views: 4128

Answers (3)

CodeGenerator
CodeGenerator

Reputation: 21

Mine worked by:

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

Upvotes: 0

S&#243;sthenes Neto
S&#243;sthenes Neto

Reputation: 27

I made a little change here in your CODEPEN DEMO, now the accessibility with keyboard is supported.

/* ... */
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);
}
/* ... */

Upvotes: 0

azz
azz

Reputation: 5930

Simple solutions are the best!

:not(:checked)

Upvotes: 7

Related Questions