main.js 4.1 KB

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