Vue Js Switch | On | Off
Vue Js Toggle switch Button: In this Vue application creates a toggle switch and updates the displayed status based on the value of the switchValue
property. When the checkbox is clicked, the value of switchValue
is updated and the display is updated accordingly.
Thanks for your feedback!
Your contributions will help us to improve service.
How to Make toggle switch in Vue Js?
The Below code is a simple Vue.js application that creates a toggle switch and displays its status (On or Off) based on its value. Here's a breakdown of the code:
-
The
<div id="app">
element serves as the root element of the Vue application. -
The
<label>
element contains an HTML checkbox input and a<span class="toggle">
element. The checkbox input is bound to theswitchValue
data property using thev-model
directive. -
The
<p>
element displays the current status of the toggle switch, either "On" or "Off". The text is dynamically updated using string templates ({{ }}
) and the ternary operator (? :
). -
The JavaScript code at the bottom of the code block uses the
createApp
function from the Vue library to create a new Vue application. -
The
data()
function returns an object that defines the initial state of the application, including theswitchValue
property, which is set tofalse
. -
The
createApp
function is then mounted on the#app
element using the.mount()
method. This associates the Vue application with the<div id="app">
element in the HTML code.
This CSS code styles the toggle switch and its associated elements in the Vue application. Here's a breakdown of the code.In this CSS code styles the toggle switch and its inner circle to create a visually appealing and functional toggle switch for the Vue application
-
The
.toggle
class defines the appearance of the toggle switch. It sets the display type toinline-block
, sets the dimensions of the toggle switch to50px
by24px
, sets the background color to#ddd
(light gray), sets the border radius to34px
to give the toggle switch a rounded appearance, sets the position torelative
to allow absolute positioning of the inner toggle circle, sets the cursor type topointer
, and sets a transition effect on the background color. -
The
input[type="checkbox"]
selector sets the display type of the checkbox input tonone
, effectively hiding it. -
The
input[type="checkbox"]:checked+.toggle
selector styles the toggle switch when the checkbox is checked. It changes the background color to#4cd964
(a green color). -
The
input[type="checkbox"]:checked+.toggle::before
selector styles the inner toggle circle when the checkbox is checked. It sets thetransform
property totranslateX(26px)
to move the circle to the right. -
The
.toggle::before
class styles the inner toggle circle. It sets the display type toblock
, sets the dimensions of the circle to20px
by20px
, sets the background color to#fff
(white), sets the border radius to50%
to make the circle a perfect circle, sets the position toabsolute
to position it relative to the toggle switch, sets the top and left position to2px
to create some space between the circle and the toggle switch, sets a transition effect on the transform property, and sets a box shadow to give the circle a slight 3D effect.