hugo-academic.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************
  2. * Hugo Academic: an academic theme for Hugo.
  3. * https://github.com/gcushen/hugo-academic
  4. **************************************************/
  5. (function($){
  6. /* ---------------------------------------------------------------------------
  7. * Add smooth scrolling to all links inside the main navbar.
  8. * --------------------------------------------------------------------------- */
  9. $('#navbar-main li.nav-item a').on('click', function(event){
  10. // Store requested URL hash.
  11. var hash = this.hash;
  12. // If we are on the homepage and the navigation bar link is to a homepage section.
  13. if( hash && $(hash).length && ($("#homepage").length > 0)){
  14. // Prevent default click behavior
  15. event.preventDefault();
  16. var navbarHeight = $('.navbar-header').innerHeight();
  17. // Use jQuery's animate() method for smooth page scrolling.
  18. // The numerical parameter specifies the time (ms) taken to scroll to the specified hash.
  19. $('html, body').animate({
  20. scrollTop: $(hash).offset().top - navbarHeight
  21. }, 800, function () {
  22. // Add hash (#) to URL once finished scrolling to hash position
  23. if (hash == "#top"){
  24. window.location.hash = ""
  25. }else {
  26. window.location.hash = hash;
  27. }
  28. });
  29. }
  30. });
  31. /* ---------------------------------------------------------------------------
  32. * Smooth scrolling for Back To Top link.
  33. * --------------------------------------------------------------------------- */
  34. $('#back_to_top').on('click', function(event){
  35. event.preventDefault();
  36. $('html, body').animate({
  37. 'scrollTop': 0
  38. }, 800, function(){
  39. window.location.hash = ""
  40. });
  41. });
  42. /* ---------------------------------------------------------------------------
  43. * Smooth scrolling for mouse wheel.
  44. * --------------------------------------------------------------------------- */
  45. function smoothScroll(scrollTime, scrollDistance){
  46. if (navigator.userAgent.indexOf('Mac') != -1 || navigator.userAgent.indexOf('Firefox') > -1 || jQuery('body').hasClass('is-horizontal')){
  47. return;
  48. }
  49. jQuery(window).on("mousewheel DOMMouseScroll", function(event){
  50. event.preventDefault();
  51. var delta = event.originalEvent.wheelDelta/120 || -event.originalEvent.detail/3;
  52. var scrollTop = jQuery(window).scrollTop();
  53. var finalScroll = scrollTop - parseInt(delta*scrollDistance);
  54. TweenMax.to(jQuery(window), scrollTime, {
  55. scrollTo : { y: finalScroll, autoKill:true },
  56. ease: Expo.easeOut,
  57. autoKill: true,
  58. overwrite: 5
  59. });
  60. });
  61. }
  62. /* ---------------------------------------------------------------------------
  63. * Hide mobile collapsable menu on clicking a link.
  64. * --------------------------------------------------------------------------- */
  65. $(document).on('click','.navbar-collapse.in',function(e){
  66. if( $(e.target).is('a') && $(e.target).attr('class') != 'dropdown-toggle' ){
  67. $(this).collapse('hide');
  68. }
  69. });
  70. /* ---------------------------------------------------------------------------
  71. * Filter projects.
  72. * --------------------------------------------------------------------------- */
  73. var $grid_projects = $('#container-projects');
  74. $grid_projects.imagesLoaded(function () {
  75. // Initialize Isotope after all images have loaded.
  76. $grid_projects.isotope({
  77. itemSelector: '.isotope-item',
  78. layoutMode: 'masonry'
  79. });
  80. // Filter items when filter link is clicked.
  81. $('#filters a').click(function () {
  82. var selector = $(this).attr('data-filter');
  83. $grid_projects.isotope({filter: selector});
  84. $(this).removeClass('active').addClass('active').siblings().removeClass('active all');
  85. return false;
  86. });
  87. });
  88. /* ---------------------------------------------------------------------------
  89. * Filter publications.
  90. * --------------------------------------------------------------------------- */
  91. var $grid_pubs = $('#container-publications');
  92. $grid_pubs.isotope({
  93. itemSelector: '.isotope-item',
  94. percentPosition: true,
  95. masonry: {
  96. // Use Bootstrap compatible grid layout.
  97. columnWidth: '.grid-sizer'
  98. }
  99. });
  100. // Bind publication filter on dropdown change.
  101. $('.pub-filters-select').on( 'change', function() {
  102. // Get filter value from option value.
  103. var filterValue = this.value;
  104. // Apply filter to Isotope.
  105. $grid_pubs.isotope({ filter: filterValue });
  106. // Set hash URL to current filter.
  107. var url = $(this).val();
  108. if (url.substr(0, 9) == '.pubtype-') {
  109. window.location.hash = url.substr(9);
  110. } else {
  111. window.location.hash = '';
  112. }
  113. });
  114. // Filter publications according to hash in URL.
  115. function filter_publications() {
  116. var urlHash = window.location.hash.replace('#','');
  117. var filterValue = '*';
  118. // Check if hash is numeric.
  119. if (urlHash != '' && !isNaN(urlHash)) {
  120. filterValue = '.pubtype-' + urlHash;
  121. }
  122. $('.pub-filters-select').val(filterValue);
  123. $grid_pubs.isotope({ filter: filterValue });
  124. }
  125. /* ---------------------------------------------------------------------------
  126. * On window load.
  127. * --------------------------------------------------------------------------- */
  128. $(window).load(function(){
  129. // When accessing publication index, enable filtering.
  130. if ($('.pub-filters-select')) {
  131. filter_publications();
  132. // Useful for changing hash manually (e.g. in development):
  133. // window.addEventListener('hashchange', filter_publications, false);
  134. }
  135. // Enable smooth scrolling with mouse wheel
  136. smoothScroll(1.3, 220);
  137. // When accessing homepage from another page and `#top` hash is set, show top of page (no hash).
  138. if (window.location.hash == "#top") {
  139. window.location.hash = ""
  140. }
  141. // Initialize Scrollspy.
  142. var $body = $('body');
  143. var $navbar = $('.navbar-header');
  144. var navbar_offset = $navbar.innerHeight() + 1;
  145. $body.scrollspy({offset: navbar_offset });
  146. // Make Scrollspy responsive.
  147. function fixScrollspy() {
  148. var data = $body.data('bs.scrollspy');
  149. if (data) {
  150. navbar_offset = $navbar.innerHeight() + 1;
  151. data.options.offset = navbar_offset;
  152. $body.data('bs.scrollspy', data);
  153. $body.scrollspy('refresh');
  154. }
  155. }
  156. // Call `fixScrollspy` when window is resized.
  157. var resizeTimer;
  158. $(window).resize(function() {
  159. clearTimeout(resizeTimer);
  160. resizeTimer = setTimeout(fixScrollspy, 200);
  161. });
  162. });
  163. })(jQuery);