slot machine

by zerrax

HTML

<div id="machine">
  <div id="0"></div>
  <div id="1"></div>
  <div id="2"></div>
  <div id="3"></div>
  <div id="4"></div>
</div>
<div id="play">
  <button>Play</button>
</div>
<img src="http://i.imgur.com/yVyLPbG.png">

CSS

@-webkit-keyframes spin {
  0% {
    background-position: 0, 0 0;
  }
  100% {
    background-position: 0, 0 -5640px;
  }
}

body {
  font-family: Arial, Helvetica, sans-serif;
}


/* machine border */

#machine {
  width: 570px;
  margin: auto;
  padding: 5px;
  overflow: hidden;
  background-color: #333;
  background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#666666), to(#666666), color-stop(.6, #333));
  border-radius: 10px;
}


/* reel */

#machine > div {
  float: left;
  width: 100px;
  margin: 7px;
  height: 320px;
  background-color: #FFF;
  background-position: 0;
  background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgba(0, 0, 0, .6)), to(rgba(0, 0, 0, .6)), color-stop(.3, rgba(0, 0, 0, 0)), color-stop(.7, rgba(0, 0, 0, 0))), url("http://i.imgur.com/yVyLPbG.png");
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -webkit-box-shadow: 0 0 5px #000 inset;
}

#play button {
  margin: 20px auto;
  text-align: center;
  display: block;
  padding: 5px;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 12px;
  font-weight: bold;
  color: #000;
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#CCC), to(#999));
  -webkit-box-shadow: 0 0 5px #000;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  text-shadow: 0 1px 1px #FFF;
}

JavaScript

$("#play").click(function() {
  var css = '-webkit-animation: spin 3s cubic-bezier(0, 0, 0.58, 1.05); animation-fill-mode: forwards;';
  spinLoop(css);
});

function spinLoop(css) {
  for (i = 0; i < 150; i++) {
    (function(i) {
      setTimeout(function() {
        $('#machine > #' + i).attr(
          'style', css
        );
      }, 1 * i);
    }(i));
  }
}