
I have created on/off switch with background changing effect Using #html #css and javascript.
Below is HTML file Code.
<div class="wrap">
<label class="switch">
<input type="checkbox" onclick="change()">
<span class="slider round">
<h1 id="light-text">OFF</h1>
</span>
</label>
</div>
CSS Code
Below is CSS file Code.
body {
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
text-align: center;
background: #ffb830;
padding: 0;
margin: 0;
color: #fff;
}
.wrap {
margin: auto;
}
.switch {
position: relative;
display: inline-block;
width: 250px;
height: 80px;
}
.switch input {
visibility: hidden;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #ffb830;
-webkit-transition: 0.4s;
transition: 0.4s;
box-shadow: -5px -5px 8px rgba(255,255,255,0.2),
5px 5px 8px rgba(0,0,0,0.2);
}
.slider:before {
position: absolute;
content: "";
height: 65px;
width: 65px;
background: #fff;
left: 4px;
bottom: 4px;
-webkit-transition: 0.5s;
transition: 0.5s;
}
input:checked + .slider {
background: #ff2442;
}
input:checked + .slider:before {
transform: translateX(177px);
}
.slider.round {
border-radius: 50px;
}
.slider.round:before {
border-radius: 50%;
}
.lights-on {
background: #ff2442;
width: 100vw;
height: 100vh;
}
Below is JS file Code.
function change() {
var x = document.getElementById("light-text");
if (x.innerHTML === "OFF") {
x.innerHTML = "ON";
} else {
x.innerHTML = "OFF";
}
var element = document.body;
element.classList.toggle("lights-on");
}
Search Your Keywords
Recent Post
- Boost Your Web Design Skills: 10 CSS Tricks That Will Blow Your Mind!10 Mind-Blowing Tricks to Elevate Your Web Design Skills As a […]
- Creating Engaging Web Animations with HTML and CSS: A Simple Snake Animation ExampleAdding animations to a web page is a fantastic way to […]
- Center a div like a Pro: 4 Essential Techniques You Must Master in CSSAre you looking to center a div on your webpage and […]
- Neumorphic Design: Revolutionize Your Login Experience with Cutting-Edge TechniquesNeumorphic design has taken the web design world by storm with […]
- Neumorphic Card Design: An Elegant UI SolutionDiscover the Power of Neumorphic Card Design for Your UI If […]