使用css链接复选框动画

时间:2015-07-20 11:59:38

标签: javascript html css css3 animation

我是CSS Animations的新手,我正在尝试链接正在检查的复选框的动画。基本上,我有一个项目列表和相应的复选框。我的目标是一个接一个地检查复选框。

我在几个在线教程的帮助下完成了检查动画(动画刻录)。虽然现在我无法找到复制复选框点击事件的方法来启动动画。

https://jsfiddle.net/vishsid73/a7L5kdvL/2/

@-moz-keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@-webkit-keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}
@-webkit-keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}
@-moz-keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}

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

.check-box {
  height: 50px;
  width: 50px;
  background-color: transparent;
  border: 5px solid black;
  border-radius: 5px;
  position: relative;
  display: inline-block;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -moz-transition: border-color ease 0.2s;
  -o-transition: border-color ease 0.2s;
  -webkit-transition: border-color ease 0.2s;
  transition: border-color ease 0.2s;
  cursor: pointer;
}

.check-box::before,
.check-box::after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: absolute;
  height: 0;
  width: 10px;
  background-color: #23527c;
  display: inline-block;
  -moz-transform-origin: left top;
  -ms-transform-origin: left top;
  -o-transform-origin: left top;
  -webkit-transform-origin: left top;
  transform-origin: left top;
  border-radius: 5px;
  content: ' ';
  -webkit-transition: opacity ease .5;
  -moz-transition: opacity ease .5;
  transition: opacity ease .5;
}

.check-box::before {
  top: 40px;
  left: 20px;
/*  box-shadow: 0 0 0 5px #667788;*/
  -moz-transform: rotate(-135deg);
  -ms-transform: rotate(-135deg);
  -o-transform: rotate(-135deg);
  -webkit-transform: rotate(-135deg);
  transform: rotate(-135deg);
}

.check-box::after {
/*  top: 37px; */
  top: 20px; 
  left: 0px;
/*  left: 5px;*/
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

input[type=checkbox]:checked + .check-box,
.check-box.checked { border-color: #23527c; }

input[type=checkbox]:checked + .check-box::after,
.check-box.checked::after {
/*  height: 50px;*/
  height: 25px;
  -moz-animation: dothabottomcheck 0.2s ease 0s forwards;
  -o-animation: dothabottomcheck 0.2s ease 0s forwards;
  -webkit-animation: dothabottomcheck 0.2s ease 0s forwards;
  animation: dothabottomcheck 0.2s ease 0s forwards;
}

input[type=checkbox]:checked + .check-box::before,
.check-box.checked::before {
/*  height: 120px;*/
  height: 70px;
  -moz-animation: dothatopcheck 0.4s ease 0s forwards;
  -o-animation: dothatopcheck 0.4s ease 0s forwards;
  -webkit-animation: dothatopcheck 0.4s ease 0s forwards;
  animation: dothatopcheck 0.4s ease 0s forwards;
}
<body>

<div class="container">
  <h2>Sid Anim</h2>
  <input type="checkbox" id="cbtest" />
    <label for="cbtest" class="check-box"></label>
    <br><br>
    <input type="checkbox" id="cbtest1" />
    <label for="cbtest1" class="check-box"></label>
    <br><br>
    <input type="checkbox" id="cbtest2" />
    <label for="cbtest2" class="check-box"></label>
    
</div>
</body>

请指导如何在之前的

之后以1.5秒的间隙链接动画

2 个答案:

答案 0 :(得分:1)

我不是自称是任何JQuery专家,但你可以这样做

&#13;
&#13;
$(document).ready(function() {
  $('#cbtest').click(function() {
    $('input').each(function(i) {
      var myDelay = 500
      var myElement = $(this);
      setTimeout(function() {
        myElement.prop('checked', true);
      }, i * myDelay);
    });
  });
});
&#13;
@-moz-keyframes dothabottomcheck {
  0% {
    height: 0;
  }
  100% {
    height: 25px;
  }
}
@-webkit-keyframes dothabottomcheck {
  0% {
    height: 0;
  }
  100% {
    height: 25px;
  }
}
@keyframes dothabottomcheck {
  0% {
    height: 0;
  }
  100% {
    height: 25px;
  }
}
@keyframes dothatopcheck {
  0% {
    height: 0;
  }
  50% {
    height: 0;
  }
  100% {
    height: 70px;
  }
}
@-webkit-keyframes dothatopcheck {
  0% {
    height: 0;
  }
  50% {
    height: 0;
  }
  100% {
    height: 70px;
  }
}
@-moz-keyframes dothatopcheck {
  0% {
    height: 0;
  }
  50% {
    height: 0;
  }
  100% {
    height: 70px;
  }
}
input[type=checkbox] {
  display: none;
}
.check-box {
  height: 50px;
  width: 50px;
  background-color: transparent;
  border: 5px solid black;
  border-radius: 5px;
  position: relative;
  display: inline-block;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -moz-transition: border-color ease 0.2s;
  -o-transition: border-color ease 0.2s;
  -webkit-transition: border-color ease 0.2s;
  transition: border-color ease 0.2s;
  cursor: pointer;
}
.check-box::before,
.check-box::after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: absolute;
  height: 0;
  width: 10px;
  background-color: #23527c;
  display: inline-block;
  -moz-transform-origin: left top;
  -ms-transform-origin: left top;
  -o-transform-origin: left top;
  -webkit-transform-origin: left top;
  transform-origin: left top;
  border-radius: 5px;
  content: ' ';
  -webkit-transition: opacity ease .5;
  -moz-transition: opacity ease .5;
  transition: opacity ease .5;
}
.check-box::before {
  top: 40px;
  left: 20px;
  /*  box-shadow: 0 0 0 5px #667788;*/
  -moz-transform: rotate(-135deg);
  -ms-transform: rotate(-135deg);
  -o-transform: rotate(-135deg);
  -webkit-transform: rotate(-135deg);
  transform: rotate(-135deg);
}
.check-box::after {
  /*  top: 37px; */
  top: 20px;
  left: 0px;
  /*  left: 5px;*/
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}
input[type=checkbox]:checked + .check-box,
.check-box.checked {
  border-color: #23527c;
}
input[type=checkbox]:checked + .check-box::after,
.check-box.checked::after {
  /*  height: 50px;*/
  height: 25px;
  -moz-animation: dothabottomcheck 0.2s ease 0s forwards;
  -o-animation: dothabottomcheck 0.2s ease 0s forwards;
  -webkit-animation: dothabottomcheck 0.2s ease 0s forwards;
  animation: dothabottomcheck 0.2s ease 0s forwards;
}
input[type=checkbox]:checked + .check-box::before,
.check-box.checked::before {
  /*  height: 120px;*/
  height: 70px;
  -moz-animation: dothatopcheck 0.4s ease 0s forwards;
  -o-animation: dothatopcheck 0.4s ease 0s forwards;
  -webkit-animation: dothatopcheck 0.4s ease 0s forwards;
  animation: dothatopcheck 0.4s ease 0s forwards;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <div class="container">
    <h2>Sid Anim</h2>

    <input type="checkbox" id="cbtest" />
    <label for="cbtest" class="check-box"></label>
    <br/>
    <br/>
    <input type="checkbox" id="cbtest1" />
    <label for="cbtest1" class="check-box"></label>
    <br/>
    <br/>
    <input type="checkbox" id="cbtest2" />
    <label for="cbtest2" class="check-box"></label>
  </div>
</body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用当前的CSS动画来实现这一点,方法是使用Javascript来检查带有延迟的复选框......

JQuery版本 如果您已在页面上使用jQuery ......

&#13;
&#13;
$('input[type="checkbox"]').each(function(i) {
    var item = $(this);
    setTimeout(function() {
        item.prop('checked','checked');
    }, 1000*i);
});
&#13;
@-moz-keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@-webkit-keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}
@-webkit-keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}
@-moz-keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}

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

.check-box {
  height: 50px;
  width: 50px;
  background-color: transparent;
  border: 5px solid black;
  border-radius: 5px;
  position: relative;
  display: inline-block;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -moz-transition: border-color ease 0.2s;
  -o-transition: border-color ease 0.2s;
  -webkit-transition: border-color ease 0.2s;
  transition: border-color ease 0.2s;
  cursor: pointer;
}

.check-box::before,
.check-box::after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: absolute;
  height: 0;
  width: 10px;
  background-color: #23527c;
  display: inline-block;
  -moz-transform-origin: left top;
  -ms-transform-origin: left top;
  -o-transform-origin: left top;
  -webkit-transform-origin: left top;
  transform-origin: left top;
  border-radius: 5px;
  content: ' ';
  -webkit-transition: opacity ease .5;
  -moz-transition: opacity ease .5;
  transition: opacity ease .5;
}

.check-box::before {
  top: 40px;
  left: 20px;
/*  box-shadow: 0 0 0 5px #667788;*/
  -moz-transform: rotate(-135deg);
  -ms-transform: rotate(-135deg);
  -o-transform: rotate(-135deg);
  -webkit-transform: rotate(-135deg);
  transform: rotate(-135deg);
}

.check-box::after {
/*  top: 37px; */
  top: 20px; 
  left: 0px;
/*  left: 5px;*/
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

input[type=checkbox]:checked + .check-box,
.check-box.checked { border-color: #23527c; }

input[type=checkbox]:checked + .check-box::after,
.check-box.checked::after {
/*  height: 50px;*/
  height: 25px;
  -moz-animation: dothabottomcheck 0.2s ease 0s forwards;
  -o-animation: dothabottomcheck 0.2s ease 0s forwards;
  -webkit-animation: dothabottomcheck 0.2s ease 0s forwards;
  animation: dothabottomcheck 0.2s ease 0s forwards;
}

input[type=checkbox]:checked + .check-box::before,
.check-box.checked::before {
/*  height: 120px;*/
  height: 70px;
  -moz-animation: dothatopcheck 0.4s ease 0s forwards;
  -o-animation: dothatopcheck 0.4s ease 0s forwards;
  -webkit-animation: dothatopcheck 0.4s ease 0s forwards;
  animation: dothatopcheck 0.4s ease 0s forwards;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>

<div class="container">
  <h2>Sid Anim</h2>
  <input type="checkbox" id="cbtest" />
    <label for="cbtest" class="check-box"></label>
    <br><br>
    <input type="checkbox" id="cbtest1" />
    <label for="cbtest1" class="check-box"></label>
    <br><br>
    <input type="checkbox" id="cbtest2" />
    <label for="cbtest2" class="check-box"></label>
    
</div>
</body>
&#13;
&#13;
&#13;

Javascript版本 如果你需要一个vanilla js解决方案......

&#13;
&#13;
checkboxes = document.getElementsByTagName("input");
for(var i=0;i < checkboxes.length;i++) {
    doSetTimeout(i)
}

function doSetTimeout(i) {
  setTimeout(function() { 
      checkboxes[i].checked = true;
  }, 1000*i);
}
&#13;
@-moz-keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@-webkit-keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@keyframes 
dothabottomcheck {  0% {
 height: 0;
}
 100% {
 height: 25px;
}
}
@keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}
@-webkit-keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}
@-moz-keyframes 
dothatopcheck {  0% {
 height: 0;
}
 50% {
 height: 0;
}
 100% {
 height: 70px;
}
}

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

.check-box {
  height: 50px;
  width: 50px;
  background-color: transparent;
  border: 5px solid black;
  border-radius: 5px;
  position: relative;
  display: inline-block;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -moz-transition: border-color ease 0.2s;
  -o-transition: border-color ease 0.2s;
  -webkit-transition: border-color ease 0.2s;
  transition: border-color ease 0.2s;
  cursor: pointer;
}

.check-box::before,
.check-box::after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: absolute;
  height: 0;
  width: 10px;
  background-color: #23527c;
  display: inline-block;
  -moz-transform-origin: left top;
  -ms-transform-origin: left top;
  -o-transform-origin: left top;
  -webkit-transform-origin: left top;
  transform-origin: left top;
  border-radius: 5px;
  content: ' ';
  -webkit-transition: opacity ease .5;
  -moz-transition: opacity ease .5;
  transition: opacity ease .5;
}

.check-box::before {
  top: 40px;
  left: 20px;
/*  box-shadow: 0 0 0 5px #667788;*/
  -moz-transform: rotate(-135deg);
  -ms-transform: rotate(-135deg);
  -o-transform: rotate(-135deg);
  -webkit-transform: rotate(-135deg);
  transform: rotate(-135deg);
}

.check-box::after {
/*  top: 37px; */
  top: 20px; 
  left: 0px;
/*  left: 5px;*/
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

input[type=checkbox]:checked + .check-box,
.check-box.checked { border-color: #23527c; }

input[type=checkbox]:checked + .check-box::after,
.check-box.checked::after {
/*  height: 50px;*/
  height: 25px;
  -moz-animation: dothabottomcheck 0.2s ease 0s forwards;
  -o-animation: dothabottomcheck 0.2s ease 0s forwards;
  -webkit-animation: dothabottomcheck 0.2s ease 0s forwards;
  animation: dothabottomcheck 0.2s ease 0s forwards;
}

input[type=checkbox]:checked + .check-box::before,
.check-box.checked::before {
/*  height: 120px;*/
  height: 70px;
  -moz-animation: dothatopcheck 0.4s ease 0s forwards;
  -o-animation: dothatopcheck 0.4s ease 0s forwards;
  -webkit-animation: dothatopcheck 0.4s ease 0s forwards;
  animation: dothatopcheck 0.4s ease 0s forwards;
}
&#13;
<body>

<div class="container">
  <h2>Sid Anim</h2>
  <input type="checkbox" id="cbtest" />
    <label for="cbtest" class="check-box"></label>
    <br><br>
    <input type="checkbox" id="cbtest1" />
    <label for="cbtest1" class="check-box"></label>
    <br><br>
    <input type="checkbox" id="cbtest2" />
    <label for="cbtest2" class="check-box"></label>
    
</div>
</body>
&#13;
&#13;
&#13;