A toggle switch button in the top right toggles from light to dark mode and vice versa. The background is an image and zoomed in.
Currently, it moves from
- left to right when switched to dark mode
- right to left when switched to light mode
But I want it to move in one direction itself. Like, suppose it's in light mode, and when I switch it to dark mode the background image shifts to the right (left > right) like it's supposed to, and then when I switch it to light mode I want the background image to go from left to right again (left > right).
Kinda like a continuous loop but with a toggle button.
Is there any way I can achieve that?
I guess it might work with keyframes and animations not sure. But is there a way to do it in JS without the need for keyframes?
Don't mind the code below cause I had to clip it from my react project so some of the CSS property might not seem needed.
let Dark = false
const DarkOrLight = () => {
const container = document.getElementsByClassName('Login-container')[0]
if (!Dark) {
container.style.backgroundPosition = 'right'
} else {
container.style.backgroundPosition = 'left'
}
Dark = !Dark
}
.toggle-container {
position: fixed;
top: 5px;
right: 5%;
}
.toggle {
appearance: none;
width: 50px;
height: 30px;
background-image: linear-gradient(120deg, #383030 0%, #22282e 100%);
cursor: pointer;
border-radius: 20px;
}
.toggle:focus {
outline: 0;
}
.toggle::after {
content: '';
background-image: linear-gradient(120deg, rgb(182, 244, 146), rgb(51, 139, 147));
position: absolute;
top: 5px;
left: 5px;
width: 25px;
height: 25px;
border-radius: 50%;
transition: all 1s ease;
}
.toggle:checked {
background-color: #b6f492;
background-image: linear-gradient(120deg, #b6f492, rgb(51, 139, 147));
}
.toggle:checked::after {
transform: translateX(23px);
background-image: linear-gradient(120deg, #383030 0%, #22282e 100%);
}
.Login-container {
position: relative;
background-image: linear-gradient(120deg, #b6f492, #338b93, #383030, #22282e);
background-size: 400%;
background-position: left;
width: 100%;
height: 100vh;
display: flex;
justify-content: end;
transition: all 1s ease;
font-family: Merriweather;
}
<div class="Login-container">
<div class="toggle-container">
<input class="toggle" type="checkbox" onClick=DarkOrLight()></input>
</div>
</div>