jquery - How to work with a single instance of an object in javascript? -


so attempting apply several different animations single class ".box". in code have single animation , i'm going add more , create array applies animations in order, current code i'm not sure how apply each animation each instance of ".box" class without making ".box 1, .box 2, .box 3" , looping through. if i'm wrong in thinking please let me know, feel there way this. here current code:

<html> <head> <meta charset="iso-8859-1"> <title>css animation practice</title>  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>   </head> <style>  html,body { height: 100%; width:100%;}  body{     margin:0px;     padding:0px; }   div{ }  .container{     text-align: center; }  #dancebutton{     position:relative;     font-size: 2.0em;     margin-top:100px;     margin-bottom:100px;     left: 50%;     right: 50%; }  .boxborder{     display:inline-block;     background-color:#d8d8d8;     margin:10px;     height: 200px;     width:200px;     border:3px solid black; }  .box{     position:relative;     background-color: blue;     height: 50px;     width: 50px;     margin:75px; }     .cleardiv{     clear:both; }  .rotate{     -webkit-animation: rotation 2s 1; }  @-webkit-keyframes rotation {     0%{         -webkit-transform: rotate(0deg);     }      100%{         -webkit-transform: rotate(360deg);     } }    </style>   <body>   <button id="dancebutton" onclick="dancefunction()" value="dancebutton">dance!</button>  <div class="cleardiv"></div>  <div class="container">      <div class="boxborder">         <div class="box"></div>     </div>      <div class="boxborder">         <div class="box"></div>     </div>      <div class="boxborder">         <div class="box"></div>     </div>      <div class="boxborder">         <div class="box"></div>     </div>      <div class="boxborder">         <div class="box"></div>     </div>      <div class="boxborder">         <div class="box"></div>     </div>      <div class="boxborder">         <div class="box"></div>     </div>      <div class="boxborder">         <div class="box"></div>     </div>  </div>    <script>  var clicked = 0;      function dancefunction(){          if(clicked == 0){                 $(".box").addclass("rotate");                 clicked = 1                 settimeout(function(){                     $(".box").removeclass("rotate");                     clicked = 0;                 }, 2000)                 }     }   </script>    </body> </html> 

you gonna have loop, make easy. example, if store animations simple array ["rotate", "rotate", "jump", ...] classes corresponding boxes, use $().each function, like

$(".box").each(funcion(index) {     var box = $(this)     box.addclass( animations[index] )     ...       box.removeclass ...     ... }) 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -