main.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // ref: https://css-tricks.com/snippets/jquery/smooth-scrolling/
  25. // Select all links with hashes
  26. $('a[href*="#"]')
  27. // Remove links that don't actually link to anything
  28. .not('[href="#"]')
  29. .not('[href="#0"]')
  30. .click(function (event) {
  31. // On-page links
  32. if (
  33. location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
  34. &&
  35. location.hostname == this.hostname
  36. ) {
  37. // Figure out element to scroll to
  38. var target = $(decodeURI(this.hash));
  39. target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
  40. // Does a scroll target exist?
  41. if (target.length) {
  42. // Only prevent default if animation is actually gonna happen
  43. event.preventDefault();
  44. let offset = 60;
  45. if (isMobile) {
  46. offset = 710;
  47. } else if (isTablet) {
  48. offset = 60;
  49. }
  50. $('html, body').animate({
  51. scrollTop: target.offset().top - offset
  52. }, 1000, function () {
  53. // Callback after animation
  54. // Must change focus!
  55. var $target = $(target);
  56. $target.focus();
  57. if ($target.is(":focus")) { // Checking if the target was focused
  58. return false;
  59. } else {
  60. $target.attr('tabindex', '-1'); // Adding tabindex for elements not focusable
  61. $target.focus(); // Set focus again
  62. };
  63. });
  64. // Add hash (#) to URL when done scrolling (default click behavior)
  65. window.location.hash = this.hash
  66. }
  67. }
  68. });
  69. }
  70. addSmoothScroll();
  71. // re-render custom functions on window resize
  72. window.onresize = function () {
  73. detectDevice();
  74. addSmoothScroll();
  75. };
  76. });
  77. })(jQuery);
  78. // Toggle sidebar on click. Here, class "hide" open the sidebar
  79. function toggleSidebar() {
  80. let sidebar = document.getElementById("sidebar-section");
  81. if (sidebar == null) {
  82. return
  83. }
  84. if (sidebar.classList.contains("hide")) {
  85. sidebar.classList.remove("hide")
  86. } else {
  87. // if toc-section is open, then close it first
  88. let toc = document.getElementById("toc-section");
  89. if (toc != null && toc.classList.contains("hide")) {
  90. toc.classList.remove("hide");
  91. }
  92. // add "hide" class
  93. sidebar.classList.add("hide")
  94. // if it is mobile device. then scroll to top.
  95. if (isMobile && sidebar.classList.contains("hide")) {
  96. document.body.scrollTop = 0;
  97. document.documentElement.scrollTop = 0;
  98. if (document.getElementById("hero-area") != null) {
  99. document.getElementById("hero-area").classList.toggle("hide");
  100. }
  101. }
  102. }
  103. if (document.getElementById("content-section") != null) {
  104. document.getElementById("content-section").classList.toggle("hide");
  105. }
  106. }
  107. // Toggle Table of Contents on click. Here, class "hide" open the toc
  108. function toggleTOC() {
  109. let toc = document.getElementById("toc-section");
  110. if (toc == null) {
  111. return
  112. }
  113. if (toc.classList.contains("hide")) {
  114. toc.classList.remove("hide");
  115. } else {
  116. // if sidebar-section is open, then close it first
  117. let sidebar = document.getElementById("sidebar-section");
  118. if (sidebar != null && sidebar.classList.contains("hide")) {
  119. sidebar.classList.remove("hide");
  120. }
  121. // add "hide" class
  122. toc.classList.add("hide")
  123. // if it is mobile device. then scroll to top.
  124. if (isMobile && toc.classList.contains("hide")) {
  125. document.body.scrollTop = 0;
  126. document.documentElement.scrollTop = 0;
  127. }
  128. }
  129. if (document.getElementById("hero-area") != null) {
  130. document.getElementById("hero-area").classList.toggle("hide");
  131. }
  132. }