Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<!-- Details: https://kodlayiruk.com/javascript-ile-slot-makinesi-vanilla-js/ -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Slot Machine</title>
</head>
<body>
    <div class="wis-container">
      <div class="machine">
        <div class="wis-header"> 
          <div class="wis-h-main">
            <div class="wis-blink-border">
              <div class="wis-h-text">kodlayiruk.com</div>
            </div>
          </div> 
          <div class="wis-h-rleg"></div>
          <div class="wis-h-lleg"></div>
        </div>
        <div class="slots">
          <ul class="slot-machine1"></ul>
          <ul class="slot-machine2"></ul>
          <ul class="slot-machine3"></ul>
        </div>
  
        <div class="winner-modal"></div>
        <div class="loser-modal"></div>
        <div id="machine-lever">
          <div class="lever-base">	  
            <div id="lever-bar" class="wis-brr"></div>
            <div id="lever-ball" class="wis-bll"></div>
            <div class="lever-chair"></div>
            <div class="lever-chair2"></div>
          </div>
        </div>
      </div>
      <p class="wis-txt"><span class="wis-starter-txt">pull the lever for start</span></p>
    </div>
</body>
<script type="text/javascript" src="./game.js" ></script>
</html>
              
            
!

CSS

              
                :root{
    --slot1Rotate: 0;
    --slot2Rotate: 0;
    --slot3Rotate: 0;
    --baseHeight: 175px;
    --primaryColor: #fc6e00;
    --secondaryColor: #fbde44;
    --thirdColor: #83b942;
  }
html, body {
  height: 100%;
  perspective: 700;
  background-color: #282a3a;
}

.wis-container{
  height: 500px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.machine{
    top: 10em;
    height: var(--baseHeight);
    width: 520px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border: 10px solid gray;
    background: linear-gradient(90deg, var(--thirdColor) 0%, var(--secondaryColor) 50%, var(--primaryColor) 100%);
}

.slots{
  display: flex;
  row-gap: 10px;
  column-gap: 20px;
  justify-content: center;
  align-items: center;
  z-index: 2;
  height: 150px;
    overflow: hidden;
 
}

.slot-machine1,.slot-machine2
,.slot-machine3{
  width: 153px;
  height: 153px;
  list-style: none;
  padding: 0;
  position:relative;
  transform-style: preserve-3d;
}

.slot1_item,.slot2_item ,.slot3_item {
  height: 150px;
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  width: 96%;
  background: rgba(255, 255, 255, 1);
  backface-visibility: hidden;
  border-radius: 20px;
  gap: 10px;
  border: 3px solid rgba(101, 101, 101,1);
}

.animation1{
  animation-name: x-spin1;
  animation-duration: 5s;
  transition-duration: 3s;
  transition-delay: 1s;
}
.animation2{
  animation-name: x-spin2;
  animation-duration: 5s;
  transition-duration: 3s;
  transition-delay: 1.5s;
}
.animation3{
  animation-name: x-spin3;
  animation-duration: 5s;
  transition-duration: 3s;
  transition-delay: 2s;
}



.image_item {
    width: 120px;
    height: 120px;
}
.item_span{visibility: visible; position: block;}

/*Winner & Loser Modal*/

.winner-modal,.loser-modal{
  width: 650px;
  height: 500px;
  z-index: 10;
  color: black;
  border-radius: 5px;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.winner-modal{
  background-color: #a5e296;
  display: none;
}
.loser-modal{
  background-color: #eb8b93;;
  display: none;
}
.modal-title{
  font-size: 2em;
  margin: 1em;
  letter-spacing: 5px;
}

.wis-code{
  background-color: rgba(245,245,245, .7);
  padding: 0.5em 1.5em;
  font-size: 2em;
  border: 3px dotted;
}
.try-again-btn{
  border: none;
  padding: 1em 2em;
  border-radius: 5px;  
  color: #e55460;

}

@keyframes x-spin1 {
  100% {
    transform: rotateX(var(--slot1Rotate));
  }
}
@keyframes x-spin2 {
  100% {
    transform: rotateX(var(--slot2Rotate));
  }
}
@keyframes x-spin3 {
  100% {
    transform: rotateX(var(--slot3Rotate));
  }
}

/* LEVER */
#machine-lever {
  position: relative;
  top: 15em;
  left: 1em;
}

#machine-lever .lever-base , .lever-chair, .lever-chair2 {

  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
  position: absolute;

}

#machine-lever > div.lever-base {
  width: 1em;
  height: 3em;
}
.lever-chair{
  background: linear-gradient(to bottom, #a2a3a7 0%, #fefefe 35%, #2e2a2b 100%);
    width: 1.5em;
    height: 4em;
    left: 0.50em;
    top: -17em;
}
.lever-chair2{
  background: linear-gradient(to bottom, #a2a3a7 0%, #fefefe 35%, #2e2a2b 100%);
    width: 1em;
    height: 3em;
    left: 2em;
    top: -16.7em;
}


#lever-bar {
    height: 6em;
    width: 0.7em;
    background: linear-gradient(to right, #a2a3a7 0%, #fefefe 35%, #2e2a2b 100%);
    border-radius: 50%/3px;
    position: absolute;
    left: 1.87em;
    bottom: 19em;

}

#lever-ball {
  width: 2.5em;
  height: 2.5em;
  background: radial-gradient(circle at 25px 20px, var(--secondaryColor) 0%, var(--primaryColor) 100%);
  border-radius: 50%;
  position: absolute;
  left: 0.85em;
  cursor: pointer;
  bottom: 24em;
  z-index: 9;

} 

.downBall{
	animation-name: ballDown;
  	animation-duration: 1s;
}

.downBar {
	animation-name: barDown;
  	animation-duration: 1s;
}
.wis-img-item{
	height: 80px;
	width: 80px;
}

@keyframes ballDown {
	0% {
		bottom:24em;
	}
	50% {
		bottom: 20em;
	}
	100%{
		bottom:24em;
	}
}
@keyframes barDown{
	0% {
		height: 6em;
	}
	50% {
		height: 0.5em;
	}
	100% {
		height: 6em;
	}
}
/*Laver*/

/*Header Start*/

.wis-h-main {
    background-color: var(--primaryColor);
    border: 5px solid black;
    border-radius: 10px;
    padding: 10px;
    position: absolute;
    top: -10em;
    width: 100%;
    left: -1em;
    z-index: 3;

}

.wis-blink-border {
    background-color: var(--secondaryColor);
    height: 100%;
    border: 10px dotted var(--thirdColor);
    padding: 5px;
    border-radius: 2px;
    outline: 5px solid var(--secondaryColor);
    width: 93%;
    display: flex;
    justify-content: center;
    align-items: center;
    
}
.wis-h-text {
    font-size: 3em;
    text-transform: uppercase;
    color: #f00;
    text-shadow: 1px 1px 1px #f00, 1px 2px 1px #f00, 1px 3px 1px #f00, 1px 4px 1px #f00, 1px 5px 1px #f00, 1px 6px 1px #f00, 1px 10px 5px rgb(16 16 16 / 50%), 1px 15px 10px rgb(16 16 16 / 40%), 1px 20px 30px rgb(16 16 16 / 30%), 1px 25px 50px rgb(16 16 16 / 20%);
}

.wis-h-rleg, .wis-h-lleg {
    width: 1.5em;
    height: 4em;
    z-index: -1;
    background: rgb(125,125,125);
    background: linear-gradient(90deg, rgba(125,125,125,1) 0%, rgba(253,253,251,1) 46%, rgba(125,125,125,1) 100%);
}

.wis-h-lleg {
    position: absolute;
    top: -4.5em;
    left: 2em;
    z-index: -0;
}

.wis-h-rleg {
  position: absolute;
    top: -4.5em;
    right: 2em;
    z-index: -0;
}
/*Header End*/

.item_span{
  visibility: hidden;
  position: absolute;
}

/*Info Scene*/
.wis-txt {
    font-family: Open Sans, Arial, San-serif;
    font-size: 1.5em;
    margin-top: 2em;
    position: relative;
    bottom: -7em;
}
.wis-starter-txt {
    background-image: linear-gradient(to bottom, #ff5823, #ff6a1b, #ff7a11, #ff8a07, #ff9900);
    padding: 1em;
    width: 100%;
    font-weight: bold;
    text-transform: uppercase;
    color: white;
    border-radius: 10px;
}


              
            
!

JS

              
                function slotMachine(){  
    /*Degiskenler Start*/
  var machine = document.querySelector('.machine');
  var slotMac1 = document.querySelector('.slot-machine1');
  var slotMac2 = document.querySelector('.slot-machine2');
  var slotMac3 = document.querySelector('.slot-machine3');
  var leverBall = document.querySelector('#lever-ball');  
  var leverBar = document.querySelector('#lever-bar');  
  var wisText = document.querySelector('.wis-txt');
  var gameCount = 3;
  var items = [],
      winRates = [],
      totalWRates = 0;
  
  var degree1 = '36deg',
      degree2 = '72deg',
      degree3 = '108deg';
  
    var root = document.querySelector(':root');
    var paneSize = 150;
    var xAngle1,
        xAngle2,
        xAngle3;
  
    /*Degiskenler END*/    
  
    /*Items Start*/
    var oc5 ='https://raw.githubusercontent.com/ktoqaloglu/Slot-Machine/master/200x200.png,10,https://raw.githubusercontent.com/ktoqaloglu/Slot-Machine/master/200x200.png,5,https://raw.githubusercontent.com/ktoqaloglu/Slot-Machine/master/200x200.png,10,https://raw.githubusercontent.com/ktoqaloglu/Slot-Machine/master/200x200.png,20';
        totalObj = oc5.split(','),
        spliterC = 0;
    /*Items END*/
  /*Hesaplama Fonksiyonları*/
  
  function compare(value1, operator, value2) {
  switch (operator) {
      case '>':   return value1 > value2;
      case '<':   return value1 < value2;
      case '>=':  return value1 >= value2;
      case '<=':  return value1 <= value2;
      case '==':  return value1 == value2;
      case '!=':  return value1 != value2;
      case '===': return value1 === value2;
      case '!==': return value1 !== value2;
  }
  }
  
  function randomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min)
  }
  var rnd = randomInt(0,100);
  
  function init(){
  
  
    for (let index = 0; index < totalObj.length; index++) {
      if(index %2 == 0){
        items[spliterC] = totalObj[index];
      }else{
              winRates[spliterC] = totalObj[index];
              spliterC++;
              totalWRates += parseInt(totalObj[index]);
      }
    }
  
    for (let index = 0; index < winRates.length; index++) {
          if(index == 0){
        winRates[index] =parseInt(winRates[index]);  
      }else{
          winRates[index] = parseInt(winRates[index-1]) + parseInt(winRates[index]);
      }
      }
  
    for (let index = 0; index < items.length; index++) {
      slotMac1.insertAdjacentHTML('beforeend',`
      <li class="slot1_item"><img class="image_item" src='`+items[index]+`'/><span class='item_span'">`+index+`</span></li>
      `);
      slotMac2.insertAdjacentHTML('beforeend',`
      <li class="slot2_item"><img class="image_item" src='`+items[index]+`'/><span class='item_span'">`+index+`</span></li>
      `);
      slotMac3.insertAdjacentHTML('beforeend',`
      <li class="slot3_item"><img class="image_item" src='`+items[index]+`'/><span class='item_span'">`+index+`</span></li>
      `);
    }
  }
  
  
  function slot1Spin(){
    root.style.setProperty('--slot1Rotate',degree1);
    var panes1 = document.querySelectorAll(".slot1_item"),
        zDepth1 = paneSize / (2 * Math.tan(Math.PI/panes1.length));
  
  for (let index = 0; index < panes1.length; index++) {
        xAngle1 = 360 / panes1.length * index;
      panes1[index].style.transform= "rotateX("+ xAngle1 +"deg) translateZ("+ zDepth1 +"px)";
    }
  
  slotMac1.addEventListener('animationend',function(){
    slotMac1.style.transform = 'rotateX('+degree1+')';
    slotMac1.classList.remove('animation1');
  });
  };
  
  
  function slot2Spin(){
    root.style.setProperty('--slot2Rotate',degree2);
    var panes2 = document.querySelectorAll('.slot2_item'),
        zDepth2 = paneSize / (2*Math.tan(Math.PI/panes2.length));
  
        for (let i =0;i<panes2.length;i++){
          xAngle2 = 360 / panes2.length * i;
          panes2[i].style.transform = "rotateX("+xAngle2+"deg) translateZ("+zDepth2+"px)";
        }
  slotMac2.addEventListener('animationend',function(){
    slotMac2.style.transform = 'rotateX('+degree2+')';
    slotMac2.classList.remove('animation2');
  });
  
  }
  
  function slot3Spin(){
    root.style.setProperty('--slot3Rotate',degree3);
    var panes3 = document.querySelectorAll('.slot3_item'),
        zDepth3 = paneSize / (2*Math.tan(Math.PI/panes3.length));
  
        for (let j =0;j<panes3.length;j++){
          xAngle3 = 360 / panes3.length * j;
          panes3[j].style.transform = "rotateX("+xAngle3+"deg) translateZ("+zDepth3+"px)";
        }
  slotMac3.addEventListener('animationend',function(){
    slotMac3.style.transform = 'rotateX('+degree3+')';
    leverBall.classList.remove('downBall');
    leverBar.classList.remove ('downBar');
    slotMac3.classList.remove('animation3');
    checkWinner();
  });
  }
  
  function  spin(){
  /*Degree Per By Item*/
  var perByItem = 360 / items.length;
  var winnerVal = 0;
  
  if(totalWRates < 100){
    var loserRate = totalWRates + (100 - totalWRates);
  }
  
  for(let index = 0;index<items.length;index++){
    if(index == 0){
          if(compare(rnd,'<=',winRates[index]) ){
              winnerVal = perByItem * index;
        degree1 = (360 + winnerVal)+'deg';
        degree2 = (720 + winnerVal) +'deg';
        degree3 = (1080 + winnerVal)+'deg';
          }
      }else{
          if(compare(rnd,'>',winRates[index-1]) && compare(rnd,'<=',winRates[index]) ){
              winnerVal = perByItem * index;
        degree1 = (360 + winnerVal)+'deg';
        degree2 = (720 + winnerVal) +'deg';
        degree3 = (1080 + winnerVal)+'deg';
          }
      }
  }
  if(compare(rnd,'>',winRates[items.length-1]) &&  compare(rnd,'<=',100)){
    winnerVal = rnd;
    degree1 =  (360 + (perByItem * randomInt(1,5))) + 'deg';
    degree2 =  (360 + (perByItem * randomInt(1,5)))+ 'deg';
    degree3 =  (360 + (perByItem * randomInt(5,6))) + 'deg';
  }
  
  slot1Spin();
  slot2Spin();
  slot3Spin();
  }
  
  function checkWinner(){
  
    var firstSlot = slotMac1.getBoundingClientRect(),
        secondSlot = slotMac2.getBoundingClientRect(),
        lastSlot = slotMac3.getBoundingClientRect(),
        loserModal = document.querySelector('.loser-modal'),
        winnerModal = document.querySelector('.winner-modal'),
  
      r1 = document.elementFromPoint(firstSlot.x+(firstSlot.width/2),firstSlot.y+(firstSlot.height/2+10)),
      r2 = document.elementFromPoint(secondSlot.x+(secondSlot.width/2),secondSlot.y+(secondSlot.height/2+10)),
      r3 = document.elementFromPoint(lastSlot.x+(lastSlot.width/2),lastSlot.y+(lastSlot.height/2+10));
    
    setTimeout(() => {
      if (r1.parentElement.textContent == r2.parentElement.textContent && r1.parentElement.textContent == r3.parentElement.textContent && rnd <= totalWRates) {
        winnerModal.innerHTML = `
        <div class="modal-title" >Tebrikler</div>
        <div class="modal-subtitle">%20 indirim kazandınız.</div>
        <div class="wis-code">F53DWE</div>
      `;
        winnerModal.style.display = 'flex';
      } else {
        loserModal.innerHTML = `
        <div class="modal-title" >Üzgünüm Kazanamadın</div>
        <button class="try-again-btn">Yeniden Dene</button>
      `;
        loserModal.style.display = 'flex';
        var againBtn = document.querySelector('.try-again-btn');
        if(gameCount > 0){
          gameCount--;
          againBtn.addEventListener('click', function () {
            rnd = randomInt(0, 100);
  
            loserModal.style.display = 'none';
  
            slotMac1.style = '';
            slotMac2.style = '';
            slotMac3.style = '';
            slotMac1.style = '';
            slotMac2.style = '';
            slotMac3.style = '';
            spin();
            wisText.innerHTML = "<span class='wis-starter-txt'> You can spin "+gameCount+" more times.</span>"
          });
        }else{
          againBtn.textContent = 'Tüm Hakların Bitti';
          againBtn.disabled = true;
        }
      }
    }, 400);
  }
  
  init();
  spin();
    /*Start Spin*/
    leverBall.addEventListener('click',function(){
    leverBall.classList.add('downBall');
    leverBar.classList.add ('downBar');
    slotMac1.classList.add('animation1');
    slotMac2.classList.add('animation2');
    slotMac3.classList.add('animation3');
  });
  }
  slotMachine();
              
            
!
999px

Console