Back
Transform the mundane toggle into a shimmering, dynamic cube that rotates on click with an eyecatching light reflection.
<div class="cube-container">
<div class="cube">
<div class="cube-face front"></div>
<div class="cube-face back"></div>
<div class="cube-face left"></div>
<div class="cube-face right"></div>
<div class="cube-face top"></div>
<div class="cube-face bottom"></div>
</div>
<button onclick="toggleCube()" class="toggle-button">
Toggle Cube
</button>
</div>
let isRotated = false;
function toggleCube() {
const cube = document.querySelector('.cube');
isRotated = !isRotated;
cube.style.transform = isRotated ? 'rotateY(180deg)' : 'rotateY(0)';
}
.cube-container {
perspective: 1000px;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
.cube {
width: 100px;
height: 100px;
position: relative;
transform-style: preserve-3d;
transition: transform 1s;
}
.cube-face {
position: absolute;
width: 100px;
height: 100px;
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.8);
backface-visibility: hidden;
}
.front { transform: translateZ(50px); }
.back { transform: rotateY(180deg) translateZ(50px); }
.left { transform: rotateY(-90deg) translateZ(50px); }
.right { transform: rotateY(90deg) translateZ(50px); }
.top { transform: rotateX(90deg) translateZ(50px); }
.bottom { transform: rotateX(-90deg) translateZ(50px); }
.toggle-button {
margin-top: 20px;
padding: 10px 20px;
background: #fff;
border-radius: 5px;
font-weight: bold;
transition: background-color 0.3s, color 0.3s;
}
.toggle-button:hover {
background-color: #e0e0e0;
color: #333;
}
The reflective cube and its interactive toggle create a playful yet functional user experience, blending modern design techniques with traditional UI components.
<div class="cube-container">
<div class="cube">
<div class="cube-face front"></div>
<div class="cube-face back"></div>
<div class="cube-face left"></div>
<div class="cube-face right"></div>
<div class="cube-face top"></div>
<div class="cube-face bottom"></div>
</div>
<button onclick="toggleCube()" class="toggle-button">
Toggle Cube
</button>
</div>
<script>
let isRotated = false;
function toggleCube() {
const cube = document.querySelector('.cube');
isRotated = !isRotated;
cube.style.transform = isRotated ? 'rotateY(180deg)' : 'rotateY(0)';
}
</script>
<style>
.cube-container {
perspective: 1000px;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
.cube {
width: 100px;
height: 100px;
position: relative;
transform-style: preserve-3d;
transition: transform 1s;
}
.cube-face {
position: absolute;
width: 100px;
height: 100px;
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.8);
backface-visibility: hidden;
}
.front { transform: translateZ(50px); }
.back { transform: rotateY(180deg) translateZ(50px); }
.left { transform: rotateY(-90deg) translateZ(50px); }
.right { transform: rotateY(90deg) translateZ(50px); }
.top { transform: rotateX(90deg) translateZ(50px); }
.bottom { transform: rotateX(-90deg) translateZ(50px); }
.toggle-button {
margin-top: 20px;
padding: 10px 20px;
background: #fff;
border-radius: 5px;
font-weight: bold;
transition: background-color 0.3s, color 0.3s;
}
.toggle-button:hover {
background-color: #e0e0e0;
color: #333;
}
</style>
Thank you for reading this article.
Comments
No comments yet. Be the first to comment!