Ajax Loader
×
HTML
<div class="roulette">
1
<div class="roulette">
2
  <div class="ball">
3
    <div class="marble"></div>
4
  </div>
5
</div>
6
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.1.0/velocity.min.js" type="text/javascript"></script>
 
CSS
$blk: #122;
1
$blk: #122;
2
$wht: #EEE;
3
$red: #C21;
4
html {
5
  font-size: 4vmin;
6
}
7
.roulette {
8
  //animation: roll 5s linear reverse infinite;
9
  background-image: url("https://i.imgur.com/Gcw8Yiw.png");
10
  background-size: cover;
11
  border-radius: 100%;
12
  display: block;
13
  height: 20rem;
14
  position: relative;
15
  width: 20rem;
16
  &:before {
17
    border-radius: 100%;
18
    display: block;
19
    position: absolute;
20
    top: 0;
21
    right: 0;
22
    bottom: 0;
23
    left: 0;
24
    content: "";
25
    background-color: rgba($blk, 0.67);
26
  }
27
  &:after {
28
    background-color: $blk;
29
    background-image: radial-gradient(50% 50%, $blk 0%, $red 100%);
30
    border-radius: 100%;
31
    content: "";
32
    display: block;
33
    margin: 2rem;
34
    position: absolute;
35
    top: 0; right: 0; bottom: 0; left: 0;
36
  }
37
}
38
.ball {
39
  //animation: roll 3s linear infinite;
40
  height: 100%;
41
  margin: 0 auto;
42
  position: relative;
43
  transform: rotateZ(0deg);
44
  width: 1rem;
45
}
46
@keyframes roll {
47
  0% {transform: rotateZ(0deg);}
48
100% {transform: rotateZ(360deg);}
49
}
50
.marble {
51
  position: relative;
52
  background: lighten($wht,85%);
53
  border-radius: 100%;
54
  border-top: 1px solid $wht;
55
  border-right: 0px solid rgba($wht,0.1);
56
  border-bottom: 2px solid rgba($blk,1);
57
  border-left: 0px solid rgba($wht,0.1);
58
  box-shadow:
59
    inset 0 -0.1rem 0.05rem -0.02rem rgba($wht,0.67), // bottom highlight
60
    inset 0 -0.2rem 0.2rem 0.2rem rgba($blk,1), // shading
61
    0 0.1rem 0.1rem -0.05rem rgba($blk,0.67); // drop shadow
62
  box-sizing: border-box;
63
  height: 1rem;
64
  margin: 0 auto;
65
  top: 0.33rem;
66
  width: 1rem;
67
}
68
.freeroll {
69
  &.ball {
70
    animation: roll 3s linear infinite;
71
  }
72
  .marble {
73
    animation: roll 3s linear reverse infinite;
74
  }
75
}
 
JavaScript
var numbers = [0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26];
1
var numbers = [0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26];
2
 
3
 
4
var rotation = 0;
5
var stopped = true;
6
var updateSpeed = 250;
7
var rouletteSpeed = 200; /* 100 is slow, 500 is fast */
8
var stopSpeed = 37;
9
function roll(e) {
10
  var el = (typeof e === "undefined") ? document.querySelectorAll(".roulette")[0] : e;
11
  rotation -= updateSpeed / 1000 * rouletteSpeed;
12
  Velocity(el, { rotateZ: [rotation, "linear"] }, { duration: updateSpeed,
13
    complete: function(elem) {
14
      rouletteSpeed = rouletteSpeed < 0 ? 0 : Math.max(rouletteSpeed - (updateSpeed / 1000 * stopSpeed), 0);
15
      if(rouletteSpeed <= 0) {
16
        rewindRotationStyle(elem[0]);
17
        rotation = rotation % 360;
18
        rouletteSpeed = 400;
19
        stopped = true;
20
      } else {
21
        roll(elem);
22
      }
23
    }
24
  });
25
}
26
 
27
function rewindRotationStyle (e) {
28
  var currentRotation = parseFloat(e.style.transform.split("rotateZ(")[1].split("deg)")[0], 10);
29
  e.style.transform = "rotateZ(" + (currentRotation % 360) + "deg)";
30
}
31
 
32
function getRandomRouletteNumber(numbersArray) {
33
  return Math.floor(Math.random() * numbersArray.length);
34
}
35
function getMarbleRotation(n, numbersArray) {
36
  // returns the rotation angle relative to the wheel
37
  var position = numbersArray.indexOf(n);
38
  return position * (360 / numbersArray.length);
39
}
40
function rotateMarbleTo(element, angle, duration) {
41
  var e = document.querySelectorAll(element)[0];
42
  Velocity(e, { rotateZ: [angle, "circOut"] }, { duration: duration, complete: function(e) {
43
  } });
44
}
45
var roulette = document.querySelectorAll(".roulette")[0];
46
roulette.addEventListener("click", function () {
47
  if(stopped) {
48
    stopped = false;
49
    roll();
50
    rotateMarbleTo(".ball", getMarbleRotation(
51
      getRandomRouletteNumber(numbers)
52
      //0
53
      , numbers) + 360 * ((Math.random() * 5 >>0) + 5), 7000);
54
  }
55
});
 

Untitled

CSSDeck G+