Sure, you can create a spinning wheel animation using CSS animations and JavaScript to control the rotation. Here's a simple example:


HTML:

```html

<div id="wheel" class="wheel">

    <div id="pointer"></div>

    <div class="segment">Value 1</div>

    <div class="segment">Value 2</div>

    <div class="segment">Value 3</div>

    <div class="segment">Value 4</div>

</div>

<button onclick="spinWheel()">Spin</button>

<div id="result"></div>

```


CSS:

```css

.wheel {

    width: 300px;

    height: 300px;

    position: relative;

    border-radius: 50%;

    border: 5px solid black;

}


.segment {

    position: absolute;

    width: 100%;

    height: 100%;

    transform-origin: 50% 100%;

    border-bottom: 5px solid black;

    text-align: center;

}


.segment:nth-child(1) { transform: rotate(0deg); }

.segment:nth-child(2) { transform: rotate(90deg); }

.segment:nth-child(3) { transform: rotate(180deg); }

.segment:nth-child(4) { transform: rotate(270deg); }


#pointer {

    position: absolute;

    top: 50%;

    left: 50%;

    transform: translate(-50%, -50%) rotate(0deg);

    width: 20px;

    height: 20px;

    background-color: red;

    border-radius: 50%;

}


@keyframes rotateWheel {

    0% { transform: rotate(0deg); }

    100% { transform: rotate(1440deg); } /* 4 full rotations */

}

```


JavaScript:

```javascript

function spinWheel() {

    var wheel = document.getElementById("wheel");

    var resultElement = document.getElementById("result");

    

    // Disable button during animation

    var spinButton = document.querySelector("button");

    spinButton.disabled = true;

    

    // Randomly select a segment

    var segments = document.getElementsByClassName("segment");

    var segmentCount = segments.length;

    var randomIndex = Math.floor(Math.random() * segmentCount);

    var selectedSegment = segments[randomIndex];

    

    // Calculate rotation angle

    var rotationAngle = (randomIndex * 90) + (Math.random() * 90); // Randomize within segment

    

    // Set animation properties

    wheel.style.animation = "rotateWheel 5s cubic-bezier(0.68, -0.55, 0.27, 1.55) forwards";

    selectedSegment.style.borderBottom = "5px solid red";

    

    // Update result after animation ends

    wheel.addEventListener("animationend", function() {

        resultElement.textContent = selectedSegment.textContent;

        wheel.style.animation = ""; // Reset animation

        selectedSegment.style.borderBottom = "5px solid black"; // Reset segment style

        spinButton.disabled = false; // Enable button

    });

}

```


This code creates a spinning wheel with four segments. When you click the "Spin" button, it randomly selects one of the segments and spins the wheel accordingly. After spinning, it displays the selected value.

Previous
Next Post »