hugo-academic.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. var hash = this.hash;
  11. var url = $(this).attr('href');
  12. if( hash && $(hash).length && (url.split('#')[0] == window.location.pathname)) {
  13. // Prevent default click behavior
  14. event.preventDefault();
  15. var navbarHeight = $('.navbar-header').innerHeight();
  16. // Use jQuery's animate() method for smooth page scrolling.
  17. // The numerical parameter specifies the time (ms) taken to scroll to the specified hash.
  18. $('html, body').animate({
  19. scrollTop: $(hash).offset().top - navbarHeight
  20. }, 800, function () {
  21. // Add hash (#) to URL once finished scrolling to hash position
  22. window.location.hash = hash;
  23. });
  24. }
  25. });
  26. /* ---------------------------------------------------------------------------
  27. * Smooth scrolling for mouse wheel.
  28. * --------------------------------------------------------------------------- */
  29. function smoothScroll(scrollTime, scrollDistance){
  30. if (navigator.userAgent.indexOf('Mac') != -1 || navigator.userAgent.indexOf('Firefox') > -1 || jQuery('body').hasClass('is-horizontal')) {
  31. return;
  32. }
  33. jQuery(window).on("mousewheel DOMMouseScroll", function(event){
  34. event.preventDefault();
  35. var delta = event.originalEvent.wheelDelta/120 || -event.originalEvent.detail/3;
  36. var scrollTop = jQuery(window).scrollTop();
  37. var finalScroll = scrollTop - parseInt(delta*scrollDistance);
  38. TweenMax.to(jQuery(window), scrollTime, {
  39. scrollTo : { y: finalScroll, autoKill:true },
  40. ease: Expo.easeOut,
  41. autoKill: true,
  42. overwrite: 5
  43. });
  44. });
  45. }
  46. /* ---------------------------------------------------------------------------
  47. * Hide mobile collapsable menu on clicking a link.
  48. * --------------------------------------------------------------------------- */
  49. $(document).on('click','.navbar-collapse.in',function(e) {
  50. if( $(e.target).is('a') && $(e.target).attr('class') != 'dropdown-toggle' ) {
  51. $(this).collapse('hide');
  52. }
  53. });
  54. /* --------------------------------------------------------------------------------------------------------------------------
  55. * On Window Load.
  56. * ----------------------------------------------------------------------------------------------------------------------- */
  57. $(window).load(function(){
  58. // Enable smooth scrolling with mouse wheel
  59. smoothScroll(1.3, 220);
  60. });
  61. })(jQuery);