hugo-academic.js 2.8 KB

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