Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

js实现摇奖动效

  1. User

    js实现摇奖动效

  2. Assistant

    以下是一些实现摇奖动效的示例代码,基于 HTML、CSS 和 JavaScript:

    HTML:

    <div id="container">
      <div id="slot-machine">
        <div class="slot" id="slot1"></div>
        <div class="slot" id="slot2"></div>
        <div class="slot" id="slot3"></div>
      </div>
      <button id="spin-button">SPIN</button>
    </div>
    

    CSS:

    #container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    
    #slot-machine {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100px;
      width: 330px;
      background-color: #eee;
      border: 1px solid #999;
      border-radius: 5px;
      overflow: hidden;
    }
    
    .slot {
      font-size: 50px;
      font-weight: bold;
      color: #333;
      width: 80px;
      height: 100%;
      text-align: center;
      background-color: #fff;
      border: 1px solid #999;
      border-radius: 5px;
      margin-right: 10px;
      position: relative;
      transition: transform 1s ease-out;
    }
    
    #spin-button {
      font-size: 24px;
      font-weight: bold;
      color: #333;
      background-color: #ffc107;
      border: none;
      border-radius: 5px;
      padding: 10px 20px;
      margin-top: 20px;
      cursor: pointer;
      transition: background-color 0.5s ease-out;
    }
    
    #spin-button:hover {
      background-color: #ff9800;
    }
    

    JavaScript:

    const slotValues = [
      '🍎', '🍊', '🍓', '🍒', '🍇', '🥝', '🍌', '🍑', '🍉', '🍈'
    ];
    
    const slot1 = document.getElementById('slot1');
    const slot2 = document.getElementById('slot2');
    const slot3 = document.getElementById('slot3');
    const spinButton = document.getElementById('spin-button');
    
    let isSpinning = false;
    
    spinButton.addEventListener('click', () => {
      if (!isSpinning) {
        isSpinning = true;
        let spinCount = 0;
        const spinInterval = setInterval(() => {
          spinCount++;
          slot1.style.transform = `translateY(-${getRandomInt(10, 100)}px)`;
          slot2.style.transform = `translateY(-${getRandomInt(10, 100)}px)`;
          slot3.style.transform = `translateY(-${getRandomInt(10, 100)}px)`;
          if (spinCount > 10) {
            clearInterval(spinInterval);
            isSpinning = false;
            const slot1Value = getRandomInt(0, slotValues.length - 1);
            const slot2Value = getRandomInt(0, slotValues.length - 1);
            const slot3Value = getRandomInt(0, slotValues.length - 1);
            slot1.innerHTML = slotValues[slot1Value];
            slot2.innerHTML = slotValues[slot2Value];
            slot3.innerHTML = slotValues[slot3Value];
            slot1.style.transform = `translateY(0)`;
            slot2.style.transform = `translateY(0)`;
            slot3.style.transform = `translateY(0)`;
            if (slot1Value === slot2Value && slot2Value === slot3Value) {
              alert('JACKPOT!');
            }
          }
        }, 100);
      }
    });
    
    function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    

    这段代码使用了 HTML、CSS、JavaScript 和一个预定义的 slotValues 数组,来创建一个简单的摇奖动效。当用户点击 SPIN 按钮时,三个槽位会开始自动滚动,直到滚动次数达到了指定的值(这里是 10 次)或达到了某些其他条件,然后它们会停止滚动,并显示一个随机的符号。如果三个槽位的符号都一样,那么将会弹出一个提示。整个动画的过渡效果使用了 CSS 的 transition 属性,然后在 JavaScript 中使用了 setInterval 和 clearInterval 函数来控制它们。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.