main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 = $(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. }
  65. }
  66. });
  67. }
  68. addSmoothScroll();
  69. // re-render custom functions on window resize
  70. window.onresize = function () {
  71. detectDevice();
  72. addSmoothScroll();
  73. };
  74. });
  75. })(jQuery);
  76. // Toggle sidebar on click. Here, class "hide" open the sidebar
  77. function toggleSidebar() {
  78. let sidebar = document.getElementById("sidebar-section");
  79. if (sidebar == null) {
  80. return
  81. }
  82. if (sidebar.classList.contains("hide")) {
  83. sidebar.classList.remove("hide")
  84. } else {
  85. // if toc-section is open, then close it first
  86. let toc = document.getElementById("toc-section");
  87. if (toc != null && toc.classList.contains("hide")) {
  88. toc.classList.remove("hide");
  89. }
  90. // add "hide" class
  91. sidebar.classList.add("hide")
  92. // if it is mobile device. then scroll to top.
  93. if (isMobile && sidebar.classList.contains("hide")) {
  94. document.body.scrollTop = 0;
  95. document.documentElement.scrollTop = 0;
  96. if (document.getElementById("hero-area") != null) {
  97. document.getElementById("hero-area").classList.toggle("hide");
  98. }
  99. }
  100. }
  101. if (document.getElementById("content-section") != null) {
  102. document.getElementById("content-section").classList.toggle("hide");
  103. }
  104. }
  105. // Toggle Table of Contents on click. Here, class "hide" open the toc
  106. function toggleTOC() {
  107. let toc = document.getElementById("toc-section");
  108. if (toc == null) {
  109. return
  110. }
  111. if (toc.classList.contains("hide")) {
  112. toc.classList.remove("hide");
  113. } else {
  114. // if sidebar-section is open, then close it first
  115. let sidebar = document.getElementById("sidebar-section");
  116. if (sidebar != null && sidebar.classList.contains("hide")) {
  117. sidebar.classList.remove("hide");
  118. }
  119. // add "hide" class
  120. toc.classList.add("hide")
  121. // if it is mobile device. then scroll to top.
  122. if (isMobile && toc.classList.contains("hide")) {
  123. document.body.scrollTop = 0;
  124. document.documentElement.scrollTop = 0;
  125. }
  126. }
  127. if (document.getElementById("hero-area") != null) {
  128. document.getElementById("hero-area").classList.toggle("hide");
  129. }
  130. }