HasanXLD

script.js

Mar 27th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. // Selecting the sidebar and buttons
  2. const sidebar = document.querySelector(".sidebar");
  3. const sidebarOpenBtn = document.querySelector("#sidebar-open");
  4. const sidebarCloseBtn = document.querySelector("#sidebar-close");
  5. const sidebarLockBtn = document.querySelector("#lock-icon");
  6.  
  7. // Function to toggle the lock state of the sidebar
  8. const toggleLock = () => {
  9. sidebar.classList.toggle("locked");
  10. // If the sidebar is not locked
  11. if (!sidebar.classList.contains("locked")) {
  12. sidebar.classList.add("hoverable");
  13. sidebarLockBtn.classList.replace("bx-lock-alt", "bx-lock-open-alt");
  14. } else {
  15. sidebar.classList.remove("hoverable");
  16. sidebarLockBtn.classList.replace("bx-lock-open-alt", "bx-lock-alt");
  17. }
  18. };
  19.  
  20. // Function to hide the sidebar when the mouse leaves
  21. const hideSidebar = () => {
  22. if (sidebar.classList.contains("hoverable")) {
  23. sidebar.classList.add("close");
  24. }
  25. };
  26.  
  27. // Function to show the sidebar when the mouse enter
  28. const showSidebar = () => {
  29. if (sidebar.classList.contains("hoverable")) {
  30. sidebar.classList.remove("close");
  31. }
  32. };
  33.  
  34. // Function to show and hide the sidebar
  35. const toggleSidebar = () => {
  36. sidebar.classList.toggle("close");
  37. };
  38.  
  39. // If the window width is less than 800px, close the sidebar and remove hoverability and lock
  40. if (window.innerWidth < 800) {
  41. sidebar.classList.add("close");
  42. sidebar.classList.remove("locked");
  43. sidebar.classList.remove("hoverable");
  44. }
  45.  
  46. // Adding event listeners to buttons and sidebar for the corresponding actions
  47. sidebarLockBtn.addEventListener("click", toggleLock);
  48. sidebar.addEventListener("mouseleave", hideSidebar);
  49. sidebar.addEventListener("mouseenter", showSidebar);
  50. sidebarOpenBtn.addEventListener("click", toggleSidebar);
  51. sidebarCloseBtn.addEventListener("click", toggleSidebar);
Add Comment
Please, Sign In to add comment