Hello developers, today in this blog you will learn how to create a Priority Toggle Button using HTML, CSS & JavaScript.
The priority toggle button is used to give priority, which allows the user to change a setting between the states. A toggle button is an input element, specifically of checkbox type.
In this blog (Priority Toggle Button), there are three toggle buttons with their state. At first, all the toggle buttons are inactive. If any two toggle button is active, the third button will automatically get inactive.
The source code of this Priority Toggle Button is given below, and you can copy the source code of this program. You can use this code of priority toggle button with your creativity and can take this project to the next level.
Priority Toggle Button[Source Code]
To make this website (Priority Toggle Button), you need to create three files: an HTML file, a CSS file & a JavaScript file. First, create an HTML file with the name of index.html and remember, you have to create a file with a .html extension.
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
* {
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
background-color: #85FFBD;
background-image: linear-gradient(45deg, #85FFBD 0%, #FFFB7D 100%);
}
.toggle-container {
display: flex;
align-items: center;
margin: 10px 0;
width: 200px;
}
.toggle {
visibility: hidden;
}
.label {
position: relative;
background-color: #ffffff;
border-radius: 50px;
cursor: pointer;
display: inline-block;
margin: 0 15px 0;
width: 80px;
height: 40px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.toggle:checked + .label {
background-color: #12192c;
}
.ball {
background: #85FFBD;
height: 34px;
width: 34px;
border-radius: 50%;
position: absolute;
top: 3px;
left: 3px;
align-items: center;
justify-content: center;
animation: slideOff 0.3s linear forwards;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.toggle:checked + .label .ball {
animation: slideOn 0.3s linear forwards;
}
@keyframes slideOn {
0% {
transform: translateX(0) scale(1);
}
50% {
transform: translateX(20px) scale(1.2);
}
100% {
transform: translateX(40px) scale(1);
}
}
@keyframes slideOff {
0% {
transform: translateX(40px) scale(1);
}
50% {
transform: translateX(20px) scale(1.2);
}
100% {
transform: translateX(0) scale(1);
}
}
.credit a{
text-decoration: none;
color: #12192c;
font-weight: 800;
}
.credit {
font-family: Verdana, Geneva, Tahoma, sans-serif;
margin: 10px;
}
const toggles = document.querySelectorAll('.toggle')
const good = document.querySelector('#good')
const cheap = document.querySelector('#cheap')
const fast = document.querySelector('#fast')
toggles.forEach(toggle => toggle.addEventListener('change', (e) => doTheTrick(e.target)))
function doTheTrick(theClickedOne) {
if(good.checked && cheap.checked && fast.checked) {
if(good === theClickedOne) {
fast.checked = false
}
if(cheap === theClickedOne) {
good.checked = false
}
if(fast === theClickedOne) {
cheap.checked = false
}
}
}
Thank you for reading our blog. If you face any problem in creating this Priority Toggle Button using HTML, CSS & JavaScript, then contact us or comment us. We’ll try to provide a solution to your problem as soon as possible.
إرسال تعليق
Thank you
Learning robo team