Spinner using html css
Prince
Posted on September 12, 2024
HTML AND JS CODE
`
<div class="wheel" id="wheel">
<div class="first">
<div class="car">Car</div>
<div class="bike">Bike</div>
</div>
<div class="second">
<div class="iphone">iPhone</div>
<div class="next">Better Luck next time</div>
</div>
</div>
<!-- Spin button -->
<button class="spin" id="spin-btn">SPIN</button>
</div>
<!-- //Adding the script: -->
<script>
const wheel = document.getElementById('wheel');
const spinBtn = document.getElementById('spin-btn');
spinBtn.addEventListener('click', () => {
// Generate a random degree for rotation (ensuring multiple full rotations)
const randomDegree = Math.floor(Math.random() * 360) + 720;
// Apply the rotation to the wheel
wheel.style.transition = 'transform 4s ease-out';
wheel.style.transform = `rotate(${randomDegree}deg)`;
});
</script>`
CSS CODE
`
body{
display: flex;
justify-content: center;
height: 100vh;
}
.wheel-container{
position: relative;
display: flex;
flex-direction: column;
align-items: center;
}
.wheel{
margin-top: 30px;
background-color: aqua;
box-shadow: 5px 5px 5px 5px #393939;
height: 400px;
width: 400px;
border-radius: 50%;
position: relative;
transition: transform 4s ease-out;
display: flex;
flex-wrap: wrap;
transform: rotate(0deg);
}
.first,.second{
display: flex;
width:100%;
}
.car,.bike,.iphone,.next{
width: 50%;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
font-size:small;
font-weight: bold;
}
.car{
background-color: red;
border-top-left-radius: 200px;
}
.bike {
background-color: rgb(86, 86, 36);
/* color: black; */
border-top-right-radius: 200px;
}
.iphone {
background-color: pink;
border-bottom-left-radius: 200px;
}
.next {
background-color: purple;
border-bottom-right-radius: 200px;
}
.pointer{
width: 0;
height: 0;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-bottom: 30px solid black;
position: absolute;
top:0px;
left:50%;
transform: translateX(-50%);
transform: rotate(180deg);
}
.spin{
background-color: green;
padding: 20px;
color:white;
font-size:large;
cursor: pointer;
margin-top: 10px;
}
.spin:hover{
background-color: goldenrod;
}`
Posted on September 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024