Kawiesh

Togglebutton

Nov 23rd, 2021 (edited)
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.01 KB | None | 0 0
  1. <html>
  2. <head>
  3. <style>
  4. #togglebar{
  5. box-sizing: border-box;
  6. width: 70px; height: 30px;
  7. position: relative;
  8. border: 2px solid;
  9. border-radius: 20px;
  10. transition: .5s linear;
  11. cursor: pointer;
  12. -webkit-tap-highlight-color: transparent;
  13. }
  14.  
  15. #togglebar.on{
  16. background: #39ff14;
  17. }
  18.  
  19. #togglebutton{
  20. height: 100%;
  21. aspect-ratio: 1;
  22. outline: 2px solid;
  23. border-radius: 50%;
  24. background: pink;
  25. position: absolute;
  26. left: 0;
  27. transition: .5s linear;
  28. cursor: pointer;
  29. -webkit-tap-highlight-color: transparent;
  30. }
  31.  
  32. #togglebutton.on{
  33. left: calc(100% - 26px);
  34. background: green;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div id="togglebar">
  40. <div id="togglebutton"></div>
  41. </div>
  42.  
  43. <script>
  44. let togglebar= document.querySelector("#togglebar");
  45. let togglebutton= document.querySelector("#togglebutton");
  46.  
  47.  
  48. togglebar.onclick= function(){
  49. this.classList.toggle("on");
  50. togglebutton.classList.toggle("on");
  51. let on= this.classList.contains("on");
  52. if(on){
  53. //Do this when ON
  54. }
  55. else{
  56. //Do this when OFF
  57. }
  58. };
  59. </script>
  60. </body>
  61. </html>
Advertisement
Add Comment
Please, Sign In to add comment