scripts.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. window.addEventListener('DOMContentLoaded', event => {
  2. // Navbar shrink function
  3. var navbarShrink = function () {
  4. const navbarCollapsible = document.body.querySelector('#mainNav');
  5. if (!navbarCollapsible) {
  6. return;
  7. }
  8. if (window.scrollY === 0) {
  9. navbarCollapsible.classList.remove('navbar-shrink')
  10. } else {
  11. navbarCollapsible.classList.add('navbar-shrink')
  12. }
  13. };
  14. // Shrink the navbar
  15. navbarShrink();
  16. // Shrink the navbar when page is scrolled
  17. document.addEventListener('scroll', navbarShrink);
  18. // Activate Bootstrap scrollspy on the main nav element
  19. const mainNav = document.body.querySelector('#mainNav');
  20. if (mainNav) {
  21. new bootstrap.ScrollSpy(document.body, {
  22. target: '#mainNav',
  23. offset: 74,
  24. });
  25. };
  26. // Collapse responsive navbar when toggler is visible
  27. const navbarToggler = document.body.querySelector('.navbar-toggler');
  28. const responsiveNavItems = [].slice.call(
  29. document.querySelectorAll('#navbarResponsive .nav-link')
  30. );
  31. responsiveNavItems.map(function (responsiveNavItem) {
  32. responsiveNavItem.addEventListener('click', () => {
  33. if (window.getComputedStyle(navbarToggler).display !== 'none') {
  34. navbarToggler.click();
  35. }
  36. });
  37. });
  38. // Activate SimpleLightbox plugin for portfolio items
  39. new SimpleLightbox({
  40. elements: '#portfolio a.portfolio-box'
  41. });
  42. function applyTheme(theme) {
  43. document.body.classList.remove("theme-auto", "theme-light", "theme-dark");
  44. document.body.classList.add(`theme-${theme}`);
  45. }
  46. document.addEventListener("DOMContentLoaded", () => {
  47. document.querySelector("#theme").addEventListener("change", function() {
  48. applyTheme(this.value);
  49. });
  50. });
  51. document.addEventListener("DOMContentLoaded", () => {
  52. const savedTheme = localStorage.getItem("theme") || "auto";
  53. applyTheme(savedTheme);
  54. for (const optionElement of document.querySelectorAll("#theme option")) {
  55. optionElement.selected = savedTheme === optionElement.value;
  56. }
  57. document.querySelector("#theme").addEventListener("change", function () {
  58. localStorage.setItem("theme", this.value);
  59. applyTheme(this.value);
  60. });
  61. });
  62. });