Priority Toggle Button using HTML, CSS & JavaScript

Priority Toggle Button using HTML, CSS & JavaScript

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.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style.css" /> <title>Project Priority selection || Learningrobo</title> </head> <body> <h2>How do you want your website project to be?</h2> <div class="toggle-container"> <input type="checkbox" id="good" class="toggle"> <label for="good" class="label"> <div class="ball"></div> </label> <span>Mordern</span> </div> <div class="toggle-container"> <input type="checkbox" id="cheap" class="toggle"> <label for="cheap" class="label"> <div class="ball"></div> </label> <span>Cheap</span> </div> <div class="toggle-container"> <input type="checkbox" id="fast" class="toggle"> <label for="fast" class="label"> <div class="ball"></div> </label> <span>Fast</span> </div> <div class="credit">Made with <span style="color:tomato;font-size:20px;">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> <script src="script.js"></script> </body> </html>
CSS provides style to an HTML page.To make the page attractive create a CSS file with the name style.css and, remember that you have to create a file with a .css 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;
  }
JavaScript makes the page work functionally. At last, create a JavaScript file with the name of script.js, and remember that you've got to make a file with a .js extension.


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
        }
    }
}
We hope you would like this Priority Toggle Button using HTML, CSS & JavaScript.

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

إرسال تعليق

Thank you
Learning robo team

Post a Comment (0)

أحدث أقدم
Learning Robo says...
code copied
Welcome