events.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* global Fluid */
  2. HTMLElement.prototype.wrap = function(wrapper) {
  3. this.parentNode.insertBefore(wrapper, this);
  4. this.parentNode.removeChild(this);
  5. wrapper.appendChild(this);
  6. };
  7. Fluid.events = {
  8. registerNavbarEvent: function() {
  9. var navbar = jQuery('#navbar');
  10. if (navbar.length === 0) {
  11. return;
  12. }
  13. var submenu = jQuery('#navbar .dropdown-menu');
  14. if (navbar.offset().top > 0) {
  15. navbar.removeClass('navbar-dark');
  16. submenu.removeClass('navbar-dark');
  17. }
  18. Fluid.utils.listenScroll(function() {
  19. navbar[navbar.offset().top > 50 ? 'addClass' : 'removeClass']('top-nav-collapse');
  20. submenu[navbar.offset().top > 50 ? 'addClass' : 'removeClass']('dropdown-collapse');
  21. if (navbar.offset().top > 0) {
  22. navbar.removeClass('navbar-dark');
  23. submenu.removeClass('navbar-dark');
  24. } else {
  25. navbar.addClass('navbar-dark');
  26. submenu.removeClass('navbar-dark');
  27. }
  28. });
  29. jQuery('#navbar-toggler-btn').on('click', function() {
  30. jQuery('.animated-icon').toggleClass('open');
  31. jQuery('#navbar').toggleClass('navbar-col-show');
  32. });
  33. },
  34. registerParallaxEvent: function() {
  35. var ph = jQuery('#banner[parallax="true"]');
  36. if (ph.length === 0) {
  37. return;
  38. }
  39. var board = jQuery('#board');
  40. if (board.length === 0) {
  41. return;
  42. }
  43. var parallax = function() {
  44. var pxv = jQuery(window).scrollTop() / 5;
  45. var offset = parseInt(board.css('margin-top'), 10);
  46. var max = 96 + offset;
  47. if (pxv > max) {
  48. pxv = max;
  49. }
  50. ph.css({
  51. transform: 'translate3d(0,' + pxv + 'px,0)'
  52. });
  53. var sideCol = jQuery('.side-col');
  54. if (sideCol) {
  55. sideCol.css({
  56. 'padding-top': pxv + 'px'
  57. });
  58. }
  59. };
  60. Fluid.utils.listenScroll(parallax);
  61. },
  62. registerScrollDownArrowEvent: function() {
  63. var scrollbar = jQuery('.scroll-down-bar');
  64. if (scrollbar.length === 0) {
  65. return;
  66. }
  67. scrollbar.on('click', function() {
  68. Fluid.utils.scrollToElement('#board', -jQuery('#navbar').height());
  69. });
  70. },
  71. registerScrollTopArrowEvent: function() {
  72. var topArrow = jQuery('#scroll-top-button');
  73. if (topArrow.length === 0) {
  74. return;
  75. }
  76. var board = jQuery('#board');
  77. if (board.length === 0) {
  78. return;
  79. }
  80. var posDisplay = false;
  81. var scrollDisplay = false;
  82. // Position
  83. var setTopArrowPos = function() {
  84. var boardRight = board[0].getClientRects()[0].right;
  85. var bodyWidth = document.body.offsetWidth;
  86. var right = bodyWidth - boardRight;
  87. posDisplay = right >= 50;
  88. topArrow.css({
  89. 'bottom': posDisplay && scrollDisplay ? '20px' : '-60px',
  90. 'right' : right - 64 + 'px'
  91. });
  92. };
  93. setTopArrowPos();
  94. jQuery(window).resize(setTopArrowPos);
  95. // Display
  96. var headerHeight = board.offset().top;
  97. Fluid.utils.listenScroll(function() {
  98. var scrollHeight = document.body.scrollTop + document.documentElement.scrollTop;
  99. scrollDisplay = scrollHeight >= headerHeight;
  100. topArrow.css({
  101. 'bottom': posDisplay && scrollDisplay ? '20px' : '-60px'
  102. });
  103. });
  104. // Click
  105. topArrow.on('click', function() {
  106. jQuery('body,html').animate({
  107. scrollTop: 0,
  108. easing : 'swing'
  109. });
  110. });
  111. },
  112. registerImageLoadedEvent: function() {
  113. if (!('NProgress' in window)) { return; }
  114. var bg = document.getElementById('banner');
  115. if (bg) {
  116. var src = bg.style.backgroundImage;
  117. var url = src.match(/\((.*?)\)/)[1].replace(/(['"])/g, '');
  118. var img = new Image();
  119. img.onload = function() {
  120. window.NProgress && window.NProgress.status !== null && window.NProgress.inc(0.2);
  121. };
  122. img.src = url;
  123. if (img.complete) { img.onload(); }
  124. }
  125. var notLazyImages = jQuery('main img:not([lazyload])');
  126. var total = notLazyImages.length;
  127. for (const img of notLazyImages) {
  128. const old = img.onload;
  129. img.onload = function() {
  130. old && old();
  131. window.NProgress && window.NProgress.status !== null && window.NProgress.inc(0.5 / total);
  132. };
  133. if (img.complete) { img.onload(); }
  134. }
  135. },
  136. registerRefreshCallback: function(callback) {
  137. if (!Array.isArray(Fluid.events._refreshCallbacks)) {
  138. Fluid.events._refreshCallbacks = [];
  139. }
  140. Fluid.events._refreshCallbacks.push(callback);
  141. },
  142. refresh: function() {
  143. if (Array.isArray(Fluid.events._refreshCallbacks)) {
  144. for (var callback of Fluid.events._refreshCallbacks) {
  145. if (callback instanceof Function) {
  146. callback();
  147. }
  148. }
  149. }
  150. },
  151. billboard: function() {
  152. if (!('console' in window)) {
  153. return;
  154. }
  155. // eslint-disable-next-line no-console
  156. console.log(`
  157. -------------------------------------------------
  158. | |
  159. | ________ __ _ __ |
  160. | |_ __ |[ | (_) | ] |
  161. | | |_ \\_| | | __ _ __ .--.| | |
  162. | | _| | |[ | | | [ |/ /'\`\\' | |
  163. | _| |_ | | | \\_/ |, | || \\__/ | |
  164. | |_____| [___]'.__.'_/[___]'.__.;__] |
  165. | |
  166. | Powered by Hexo x Fluid |
  167. | https://github.com/fluid-dev/hexo-theme-fluid |
  168. | |
  169. -------------------------------------------------
  170. `);
  171. }
  172. };