script.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. // element toggle function
  3. const elementToggleFunc = function (elem) { elem.classList.toggle("active"); }
  4. // sidebar variables
  5. const sidebar = document.querySelector("[data-sidebar]");
  6. const sidebarBtn = document.querySelector("[data-sidebar-btn]");
  7. // sidebar toggle functionality for mobile
  8. sidebarBtn.addEventListener("click", function () { elementToggleFunc(sidebar); });
  9. // testimonials variables
  10. const testimonialsItem = document.querySelectorAll("[data-testimonials-item]");
  11. const modalContainer = document.querySelector("[data-modal-container]");
  12. const modalCloseBtn = document.querySelector("[data-modal-close-btn]");
  13. const overlay = document.querySelector("[data-overlay]");
  14. // modal variable
  15. const modalImg = document.querySelector("[data-modal-img]");
  16. const modalTitle = document.querySelector("[data-modal-title]");
  17. const modalText = document.querySelector("[data-modal-text]");
  18. // modal toggle function
  19. const testimonialsModalFunc = function () {
  20. modalContainer.classList.toggle("active");
  21. overlay.classList.toggle("active");
  22. }
  23. // add click event to all modal items
  24. for (let i = 0; i < testimonialsItem.length; i++) {
  25. testimonialsItem[i].addEventListener("click", function () {
  26. modalImg.src = this.querySelector("[data-testimonials-avatar]").src;
  27. modalImg.alt = this.querySelector("[data-testimonials-avatar]").alt;
  28. modalTitle.innerHTML = this.querySelector("[data-testimonials-title]").innerHTML;
  29. modalText.innerHTML = this.querySelector("[data-testimonials-text]").innerHTML;
  30. testimonialsModalFunc();
  31. });
  32. }
  33. // add click event to modal close button
  34. modalCloseBtn.addEventListener("click", testimonialsModalFunc);
  35. overlay.addEventListener("click", testimonialsModalFunc);
  36. // custom select variables
  37. const select = document.querySelector("[data-select]");
  38. const selectItems = document.querySelectorAll("[data-select-item]");
  39. const selectValue = document.querySelector("[data-selecct-value]");
  40. const filterBtn = document.querySelectorAll("[data-filter-btn]");
  41. select.addEventListener("click", function () { elementToggleFunc(this); });
  42. // add event in all select items
  43. for (let i = 0; i < selectItems.length; i++) {
  44. selectItems[i].addEventListener("click", function () {
  45. let selectedValue = this.innerText.toLowerCase();
  46. selectValue.innerText = this.innerText;
  47. elementToggleFunc(select);
  48. filterFunc(selectedValue);
  49. });
  50. }
  51. // filter variables
  52. const filterItems = document.querySelectorAll("[data-filter-item]");
  53. const filterFunc = function (selectedValue) {
  54. for (let i = 0; i < filterItems.length; i++) {
  55. if (selectedValue === "all") {
  56. filterItems[i].classList.add("active");
  57. } else if (selectedValue === filterItems[i].dataset.category) {
  58. filterItems[i].classList.add("active");
  59. } else {
  60. filterItems[i].classList.remove("active");
  61. }
  62. }
  63. }
  64. // add event in all filter button items for large screen
  65. let lastClickedBtn = filterBtn[0];
  66. for (let i = 0; i < filterBtn.length; i++) {
  67. filterBtn[i].addEventListener("click", function () {
  68. let selectedValue = this.innerText.toLowerCase();
  69. selectValue.innerText = this.innerText;
  70. filterFunc(selectedValue);
  71. lastClickedBtn.classList.remove("active");
  72. this.classList.add("active");
  73. lastClickedBtn = this;
  74. });
  75. }
  76. // contact form variables
  77. const form = document.querySelector("[data-form]");
  78. const formInputs = document.querySelectorAll("[data-form-input]");
  79. const formBtn = document.querySelector("[data-form-btn]");
  80. // add event to all form input field
  81. for (let i = 0; i < formInputs.length; i++) {
  82. formInputs[i].addEventListener("input", function () {
  83. // check form validation
  84. if (form.checkValidity()) {
  85. formBtn.removeAttribute("disabled");
  86. } else {
  87. formBtn.setAttribute("disabled", "");
  88. }
  89. });
  90. }
  91. // page navigation variables
  92. const navigationLinks = document.querySelectorAll("[data-nav-link]");
  93. const pages = document.querySelectorAll("[data-page]");
  94. // add event to all nav link
  95. for (let i = 0; i < navigationLinks.length; i++) {
  96. navigationLinks[i].addEventListener("click", function () {
  97. for (let i = 0; i < pages.length; i++) {
  98. if (this.innerHTML.toLowerCase() === pages[i].dataset.page) {
  99. pages[i].classList.add("active");
  100. navigationLinks[i].classList.add("active");
  101. window.scrollTo(0, 0);
  102. } else {
  103. pages[i].classList.remove("active");
  104. navigationLinks[i].classList.remove("active");
  105. }
  106. }
  107. });
  108. }