ractive js toggle button

23 views
Skip to first unread message

Aarga Samuel

unread,
Mar 23, 2022, 1:43:15 AM3/23/22
to Ractive.js
Hello guys I am having a tough time to create a toggle button with ractive js. they is no active transition on the button and that is a feature i will really need from the button.
Bellow is the code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>lightB</title>
<link rel="stylesheet" href="./lightB.css">
</head>
<body>
<div id='container'></div>
<script id='template' type='text/ractive'>
<div class='container'>
<div class="text">{{text}}</div>
<div class='toggle'>
{{toggle}}
<div class='toggle-btn' on-click={{Animatedtoggle()}}>
</div>
</div>

</div>
</script>
<script src="./lightB.js"></script>
</body>
</html> RACTIVE JS:
ractive = new Ractive({
el: '#container',
template: '#template',
data: { text: 'OFF',

oninit: function Animatedtoggle(){
toggle.classList.toggle('active');

if(toggle.classList.contains('active')){
text.innerHTML = "ON";
}
else {
text.innerHTML = "OFF";

}
}
}
});
let toggle = document.querySelector(".toggle");
let text = document.querySelector(".text");
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
height: 100vh;
position: relative;
background: aquamarine;
}
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
height: 50px;
display: flex;
align-items: center;
justify-content: center;

}
.container .toggle {
position: absolute;
top: 0;
left: 0;
width: 90px;
height: 40px;
background: rgb(8, 0, 15);
border-radius: 55px;
cursor: pointer;
transition: 0.3;
}

.container .toggle .toggle-btn {
position: absolute;
top: 5px;
left: 5px;
width: 40px;
height: 30px;
background: rgb(132, 115, 148);
border-radius: 25px;
transition: 0.3;
}
.container .toggle.active {
background:whitesmoke;

}

.container .toggle.active .toggle-btn {
left: 40px;
}
.text {
position: absolute;
left: 2.5cm;
font-size: 1.5rem;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
margin-left: 8px;

}


Reply all
Reply to author
Forward
0 new messages