HTML
For HTML we have a circle which triggers a toggle() function on click and βFFβ text, which will change to βNβ and vice versa.
<div class="container">
<div class="toggle" onclick="toggle()">
<div class="circle"></div>
</div>
<span class="text">FF</span>
</div>
JAVASCRIPT
For javascript, first weβll create a variable in which weβll store toggle status.
Weβll set it to false for now.
let active = false
Now weβll create a toggle function, which will be triggered on circle click.
At the beginning, we are selecting the toggle and text element using querySelector method.
Then weβll change the active status to the opposite of itβs current value, by setting the exclamation mark (β!β) in front of it (!active). If active was true, it will now become false and vice versa.
Now, if active is true, weβll add the active class to toggle element and change βFFβ text to βNβ.
If itβs false, weβll remove active class from toggle element and set text to βFFβ.
function toggle() {
let toggle = document.querySelector('.toggle')
let text = document.querySelector('.text')
active = !active
if (active) {
toggle.classList.add('active')
text.innerHTML = 'N'
} else {
toggle.classList.remove('active')
text.innerHTML = 'FF'
}
}
CSS
For CSS, weβll start by setting font size and color to text element.
.text {
font-size: 23px;
color: #494949;
}
Toggle element will be positioned relative with 40x20px size (twice as wide as circle β which weβll set later).
Weβll set rounded border to dark dray as if toggle is off.
Then weβll display everything in center using flexbox.
Lastly, Iβm setting cursor to pointer, because this element is clickable, triggers the on/off button, and Iβm adding transition so that toggle is nice and smooth.
.toggle {
position: relative;
width: 40px;
height: 20px;
border: 2px solid #494949;
border-radius: 20px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
transition: .3s;
}
Circle will be positioned absolute with left set to zero. This is the off status.Weβll set the width and height to 20 pixels (half width of itβs parent).
Now add some border radius and set the background color to dark grey. This color will become green when toggle is clicked.
Donβt forget the transition.
.circle {
position: absolute;
left: 0;
width: 20px;
height: 20px;
border-radius: 20px;
background-color: #494949;
transition: .3s;
}
Now weβre styling the active class.This class is added to toggle element when active status.
Weβll just set the border color to green.
.active {
border-color: rgb(85, 227, 180);
}
Now weβll select the sibling using the plus selector (β+β) and set itβs color to green. We are talking about βNβ text here.
.active + .text {
color: rgb(85, 227, 180);
}
Lastly, weβll style the circle when active.
Weβll move it to the right side of itβs container by setting the left property to 100% and translate X by -100%.
Weβll also change the background color to green and add some transition.
.active .circle {
left: 100%;
transform: translateX(-100%);
transition: .3s;
background-color: rgb(85, 227, 180);
}
And thatβs it.
You can find the video tutorial and the source code here.
Thank you for reading. β€οΈ
Top comments (2)
Hello ! Don't hesitate to put colors on your
codeblock
like this example for have to have a better understanding of your code πDidn't know that! Thank you! π₯π