main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. var projectCards;
  3. var isMobile = false, isTablet = false, isLaptop = false;
  4. (function ($) {
  5. jQuery(document).ready(function () {
  6. function detectDevice() {
  7. if (window.innerWidth <= 425) {
  8. isMobile = true;
  9. isTablet = false;
  10. isLaptop = false;
  11. } else if (window.innerWidth <= 768) {
  12. isMobile = false;
  13. isTablet = true;
  14. isLaptop = false;
  15. } else {
  16. isMobile = false;
  17. isTablet = false;
  18. isLaptop = true;
  19. }
  20. }
  21. detectDevice();
  22. // ================= Smooth Scroll ===================
  23. function addSmoothScroll() {
  24. // Add smooth scrolling to all links
  25. $('a[href*="#"]').on('click', function (event) {
  26. // Make sure this.hash has a value before overriding default behavior
  27. if (this.hash !== "") {
  28. // Prevent default anchor click behavior
  29. event.preventDefault();
  30. // Store hash
  31. var hash = this.hash;
  32. let offset = 60;
  33. if (isMobile) {
  34. offset = 760;
  35. } else if (isTablet) {
  36. offset = 60;
  37. }
  38. // Using jQuery's animate() method to add smooth page scroll
  39. // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
  40. $('html, body').animate({
  41. scrollTop: $(hash).offset().top - offset
  42. }, 800, function () {
  43. // Add hash (#) to URL when done scrolling (default click behavior)
  44. window.location.hash = hash
  45. });
  46. }
  47. });
  48. }
  49. addSmoothScroll();
  50. // re-render custom functions on window resize
  51. window.onresize = function () {
  52. detectDevice();
  53. addSmoothScroll();
  54. };
  55. });
  56. })(jQuery);
  57. // Toggle sidebar on click. Here, class "hide" open the sidebar
  58. function toggleSidebar() {
  59. let sidebar = document.getElementById("sidebar-section");
  60. if (sidebar == null) {
  61. return
  62. }
  63. if (sidebar.classList.contains("hide")) {
  64. sidebar.classList.remove("hide")
  65. } else {
  66. // if toc-section is open, then close it first
  67. let toc = document.getElementById("toc-section");
  68. if (toc != null && toc.classList.contains("hide")) {
  69. toc.classList.remove("hide");
  70. }
  71. // add "hide" class
  72. sidebar.classList.add("hide")
  73. // if it is mobile device. then scroll to top.
  74. if (isMobile && sidebar.classList.contains("hide")) {
  75. document.body.scrollTop = 0;
  76. document.documentElement.scrollTop = 0;
  77. if (document.getElementById("hero-area") != null) {
  78. document.getElementById("hero-area").classList.toggle("hide");
  79. }
  80. }
  81. }
  82. if (document.getElementById("content-section") != null) {
  83. document.getElementById("content-section").classList.toggle("hide");
  84. }
  85. }
  86. // Toggle Table of Contents on click. Here, class "hide" open the toc
  87. function toggleTOC() {
  88. let toc = document.getElementById("toc-section");
  89. if (toc == null) {
  90. return
  91. }
  92. if (toc.classList.contains("hide")) {
  93. toc.classList.remove("hide");
  94. } else {
  95. // if sidebar-section is open, then close it first
  96. let sidebar = document.getElementById("sidebar-section");
  97. if (sidebar != null && sidebar.classList.contains("hide")) {
  98. sidebar.classList.remove("hide");
  99. }
  100. // add "hide" class
  101. toc.classList.add("hide")
  102. // if it is mobile device. then scroll to top.
  103. if (isMobile && toc.classList.contains("hide")) {
  104. document.body.scrollTop = 0;
  105. document.documentElement.scrollTop = 0;
  106. }
  107. }
  108. if (document.getElementById("hero-area") != null) {
  109. document.getElementById("hero-area").classList.toggle("hide");
  110. }
  111. }