▲Wirig Diagram Hot
▲ ▲ ▼

"Blue & Grey Toggle Switch Button Animation"

By

Animating toggle switch buttons in modern web design can elevate user interaction and visual appeal. Let's delve into creating an engaging toggle switch button animation using blue and grey colors.

First, we'll introduce the basic structure of a toggle switch button, followed by step-by-step guidance on animating it with CSS. We'll explore two main topics: creating the button and animating the switch. Dive in to learn how to captivate users with your button's movement.

Setting Up the Toggle Switch Button

The foundation of our animated toggle switch is a basic HTML structure with two buttons and a label.

Download Animation of toggle switch. slider button turning grey and blue. on or off concept. for free
Download Animation of toggle switch. slider button turning grey and blue. on or off concept. for free
Animated Switch
Animated Switch
ToggleSwitch
ToggleSwitch

Here's a simple HTML layout to start with:

<label class="switch"> <input type="checkbox"> <span class="slider round"></span> </label>

Styling the Base Button

Let's begin by styling the base button using CSS. We'll use blue for the slider and grey for the background. Add the following styles:

an object is shown in the shape of a circle
an object is shown in the shape of a circle
4 Ways to Use Functional Animation in UI Design
4 Ways to Use Functional Animation in UI Design
a button with a check mark on it
a button with a check mark on it
.switch { position: relative; display: inline-block; width: 60px; height: 34px; } .switch input { opacity: 0; width: 0; height: 0; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; } .slider.round { border-radius: 34px; }

arja The Slider Color with JavaScript

Next, we'll change the slider's color when the button is toggled on or off using JavaScript. Add this code:

const switchButton = document.querySelector('.switch input'); switchButton.addEventListener('change', function () { if (this.checked) { document.querySelector('.slider').style.backgroundColor = '#0066c0'; } else { document.querySelector('.slider').style.backgroundColor = '#ccc'; } });

Animating the Toggle Switch

Now that our toggle switch is functional, let's animate the slider using CSS transitions.

We'll add a transition effect to the slider's left property. This will create a smooth horizontal slide when the button is toggled:

Switch 02 - 2D animation
Switch 02 - 2D animation
Day And Night Toggle Switch Animation
Day And Night Toggle Switch Animation
Switch - 2D animation
Switch - 2D animation
an image of a blue button on a white background
an image of a blue button on a white background
UI Design Light Mode Dark Mode Toggle Switch Animation
UI Design Light Mode Dark Mode Toggle Switch Animation
Day & Night Toggle button
Day & Night Toggle button
.switch { position: relative; display: inline-block; width: 60px; height: 34px; } .switch input { opacity: 0; width: 0; height: 0; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s all; transition-property: left; } .slider.round { border-radius: 34px; }

Animating the Slider Position

The switch button's checkbox position needs to change from left to right when toggled. Add this JavaScript event listener:

switchButton.addEventListener('change', function () { if (this.checked) { document.querySelector('.slider').style.left = '26px'; document.querySelector('.slider').style.backgroundColor = '#0066c0'; } else { document.querySelector('.slider').style.left = '0'; document.querySelector('.slider').style.backgroundColor = '#ccc'; } });

With these steps, you now have a captivating toggle switch button animation using blue and grey colors. Experiment with different colors and transition timings to create unique and engaging user experiences on your web pages.

Happy coding! Don't forget to share your creations and challenge your peers to build upon this effect. The world of web design is your canvas, so let your imagination run wild!

아티스트가 디자인한 토글 버튼 종의 무료 아이콘
아티스트가 디자인한 토글 버튼 종의 무료 아이콘
Day and Night Toggle
Day and Night Toggle
Toggle button #light #dark mode
Toggle button #light #dark mode
💬 Comments ↗ Share ★ Save
AD