Back
Explore the world of playful interfaces with a toggle switch that defies gravity, giving your settings panel a whimsical touch.
<div class="gravity-switch"> <div class="switch-container"> <div class="switch-left">OFF</div> <div class="switch-ball"></div> <div class="switch-right">ON</div> </div> </div>
document.querySelector('.gravity-switch').addEventListener('click', function() { let container = document.querySelector('.switch-container'); container.classList.toggle('active'); });
.gravity-switch { display: flex; justify-content: center; align-items: center; width: 200px; height: 50px; background: linear-gradient(145deg, #e6e6e6, #ffffff); border-radius: 25px; box-shadow: 4px 4px 8px #aaaaaa, -4px -4px 8px #ffffff; cursor: pointer; margin: 20px auto; }The circular "switch-ball", colored and set to transition smoothly, enhances the sense of being pulled by gravity when the switch is activated by the user. Utilizing properties like position and transition changes the layout dynamically.
.switch-container { position: relative; display: flex; width: 100%; height: 100%; border-radius: 25px; overflow: hidden; background: #f7f7f7; transition: background-color 0.5s; } .switch-ball { position: absolute; top: 50%; left: 5px; width: 40px; height: 40px; background-color: #02a8f4; border-radius: 50%; transform: translateY(-50%); transition: left 0.5s, background-color 0.5s; } .switch-left, .switch-right { display: flex; justify-content: center; align-items: center; width: 50%; font-weight: bold; color: #bbbbbb; transition: color 0.5s; } .switch-container.active > .switch-ball { left: calc(100% - 45px); background-color: #32ffcc; } .switch-container.active > .switch-left { color: #02a8f4; } .switch-container.active > .switch-right { color: #32ffcc; }This creative use of movement and transition reflects a more engaging and interactive user experience, ultimately adding a dash of magic to routine settings adjustment.
<div class="gravity-switch"> <div class="switch-container"> <div class="switch-left">OFF</div> <div class="switch-ball"></div> <div class="switch-right">ON</div> </div> </div> <script> document.querySelector('.gravity-switch').addEventListener('click', function() { let container = document.querySelector('.switch-container'); container.classList.toggle('active'); }); </script> <style> .gravity-switch { display: flex; justify-content: center; align-items: center; width: 200px; height: 50px; background: linear-gradient(145deg, #e6e6e6, #ffffff); border-radius: 25px; box-shadow: 4px 4px 8px #aaaaaa, -4px -4px 8px #ffffff; cursor: pointer; margin: 20px auto; } .switch-container { position: relative; display: flex; width: 100%; height: 100%; border-radius: 25px; overflow: hidden; background: #f7f7f7; transition: background-color 0.5s; } .switch-ball { position: absolute; top: 50%; left: 5px; width: 40px; height: 40px; background-color: #02a8f4; border-radius: 50%; transform: translateY(-50%); transition: left 0.5s, background-color 0.5s; } .switch-left, .switch-right { display: flex; justify-content: center; align-items: center; width: 50%; font-weight: bold; color: #bbbbbb; transition: color 0.5s; } .switch-container.active > .switch-ball { left: calc(100% - 45px); background-color: #32ffcc; } .switch-container.active > .switch-left { color: #02a8f4; } .switch-container.active > .switch-right { color: #32ffcc; } </style>Thank you for reading this article.
Comments
No comments yet. Be the first to comment!