hugo-academic.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 $container = $('#container-projects');
  74. $container.imagesLoaded(function () {
  75. // Initialize Isotope after all images have loaded.
  76. $container.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. $container.isotope({filter: selector});
  84. $(this).removeClass('active').addClass('active').siblings().removeClass('active all');
  85. return false;
  86. });
  87. });
  88. /* ---------------------------------------------------------------------------
  89. * On window load.
  90. * --------------------------------------------------------------------------- */
  91. $(window).load(function(){
  92. // Enable smooth scrolling with mouse wheel
  93. smoothScroll(1.3, 220);
  94. // When accessing homepage from another page and `#top` hash is set, show top of page (no hash).
  95. if (window.location.hash == "#top") {
  96. window.location.hash = ""
  97. }
  98. // Initialize Scrollspy.
  99. var $body = $('body');
  100. var $navbar = $('.navbar-header');
  101. var navbar_offset = $navbar.innerHeight() + 1;
  102. $body.scrollspy({offset: navbar_offset });
  103. // Make Scrollspy responsive.
  104. function fixScrollspy() {
  105. var data = $body.data('bs.scrollspy');
  106. if (data) {
  107. navbar_offset = $navbar.innerHeight() + 1;
  108. data.options.offset = navbar_offset;
  109. $body.data('bs.scrollspy', data);
  110. $body.scrollspy('refresh');
  111. }
  112. }
  113. // Call `fixScrollspy` when window is resized.
  114. var resizeTimer;
  115. $(window).resize(function() {
  116. clearTimeout(resizeTimer);
  117. resizeTimer = setTimeout(fixScrollspy, 200);
  118. });
  119. });
  120. })(jQuery);