hugo-academic.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * On window load.
  72. * --------------------------------------------------------------------------- */
  73. $(window).load(function(){
  74. // Enable smooth scrolling with mouse wheel
  75. smoothScroll(1.3, 220);
  76. // When accessing homepage from another page and `#top` hash is set, show top of page (no hash).
  77. if (window.location.hash == "#top") {
  78. window.location.hash = ""
  79. }
  80. });
  81. })(jQuery);