app.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* eslint-disable */
  2. var customSearch;
  3. (function ($) {
  4. "use strict";
  5. const scrollCorrection = 70; // (header height = 50px) + (gap = 20px)
  6. function scrolltoElement(elem, correction) {
  7. correction = correction || scrollCorrection;
  8. const $elem = elem.href ? $(elem.getAttribute('href')) : $(elem);
  9. $('html, body').animate({ 'scrollTop': $elem.offset().top - correction }, 400);
  10. };
  11. function setHeader() {
  12. if (!window.subData) return;
  13. const $wrapper = $('header .wrapper');
  14. const $comment = $('.s-comment', $wrapper);
  15. const $toc = $('.s-toc', $wrapper);
  16. const $top = $('.s-top',$wrapper);
  17. $wrapper.find('.nav-sub .logo').text(window.subData.title);
  18. let pos = document.body.scrollTop;
  19. $(document, window).scroll(() => {
  20. const scrollTop = $(window).scrollTop();
  21. const del = scrollTop - pos;
  22. if (del >= 20) {
  23. pos = scrollTop;
  24. $wrapper.addClass('sub');
  25. } else if (del <= -20) {
  26. pos = scrollTop;
  27. $wrapper.removeClass('sub');
  28. }
  29. });
  30. // bind events to every btn
  31. const $commentTarget = $('#comments');
  32. if ($commentTarget.length) {
  33. $comment.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($commentTarget); });
  34. } else $comment.remove();
  35. const $tocTarget = $('.toc-wrapper');
  36. if ($tocTarget.length && $tocTarget.children().length) {
  37. $toc.click((e) => { e.stopPropagation(); $tocTarget.toggleClass('active'); });
  38. } else $toc.remove();
  39. $top.click(()=>scrolltoElement(document.body));
  40. }
  41. function setHeaderMenu() {
  42. var $headerMenu = $('header .menu');
  43. var $underline = $headerMenu.find('.underline');
  44. function setUnderline($item, transition) {
  45. $item = $item || $headerMenu.find('li a.active');//get instant
  46. transition = transition === undefined ? true : !!transition;
  47. if (!transition) $underline.addClass('disable-trans');
  48. if ($item && $item.length) {
  49. $item.addClass('active').siblings().removeClass('active');
  50. $underline.css({
  51. left: $item.position().left,
  52. width: $item.innerWidth()
  53. });
  54. } else {
  55. $underline.css({
  56. left: 0,
  57. width: 0
  58. });
  59. }
  60. if (!transition) {
  61. setTimeout(function () { $underline.removeClass('disable-trans') }, 0);//get into the queue.
  62. }
  63. }
  64. $headerMenu.on('mouseenter', 'li', function (e) {
  65. setUnderline($(e.currentTarget));
  66. });
  67. $headerMenu.on('mouseout', function () {
  68. setUnderline();
  69. });
  70. //set current active nav
  71. var $active_link = null;
  72. if (location.pathname === '/' || location.pathname.startsWith('/page/')) {
  73. $active_link = $('.nav-home', $headerMenu);
  74. } else {
  75. var name = location.pathname.match(/\/(.*?)\//);
  76. if (name.length > 1) {
  77. $active_link = $('.nav-' + name[1], $headerMenu);
  78. }
  79. }
  80. setUnderline($active_link, false);
  81. }
  82. function setHeaderMenuPhone() {
  83. var $switcher = $('.l_header .switcher .s-menu');
  84. $switcher.click(function (e) {
  85. e.stopPropagation();
  86. $('body').toggleClass('z_menu-open');
  87. $switcher.toggleClass('active');
  88. });
  89. $(document).click(function (e) {
  90. $('body').removeClass('z_menu-open');
  91. $switcher.removeClass('active');
  92. });
  93. }
  94. function setHeaderSearch() {
  95. var $switcher = $('.l_header .switcher .s-search');
  96. var $header = $('.l_header');
  97. var $search = $('.l_header .m_search');
  98. if ($switcher.length === 0) return;
  99. $switcher.click(function (e) {
  100. e.stopPropagation();
  101. $header.toggleClass('z_search-open');
  102. $search.find('input').focus();
  103. });
  104. $(document).click(function (e) {
  105. $header.removeClass('z_search-open');
  106. });
  107. $search.click(function (e) {
  108. e.stopPropagation();
  109. })
  110. }
  111. function setWaves() {
  112. Waves.attach('.flat-btn', ['waves-button']);
  113. Waves.attach('.float-btn', ['waves-button', 'waves-float']);
  114. Waves.attach('.float-btn-light', ['waves-button', 'waves-float', 'waves-light']);
  115. Waves.attach('.flat-box', ['waves-block']);
  116. Waves.attach('.float-box', ['waves-block', 'waves-float']);
  117. Waves.attach('.waves-image');
  118. Waves.init();
  119. }
  120. function setScrollReveal() {
  121. const $reveal = $('.reveal');
  122. if ($reveal.length === 0) return;
  123. const sr = ScrollReveal({ distance: 0 });
  124. sr.reveal('.reveal');
  125. }
  126. function setTocToggle() {
  127. const $toc = $('.toc-wrapper');
  128. if ($toc.length === 0) return;
  129. $toc.click((e) => { e.stopPropagation(); $toc.addClass('active'); });
  130. $(document).click(() => $toc.removeClass('active'));
  131. $toc.on('click', 'a', (e) => {
  132. e.preventDefault();
  133. e.stopPropagation();
  134. scrolltoElement(e.target.tagName.toLowerCase === 'a' ? e.target : e.target.parentElement);
  135. });
  136. const liElements = Array.from($toc.find('li a'));
  137. //function animate above will convert float to int.
  138. const getAnchor = () => liElements.map(elem => Math.floor($(elem.getAttribute('href')).offset().top - scrollCorrection));
  139. let anchor = getAnchor();
  140. const scrollListener = () => {
  141. const scrollTop = $('html').scrollTop() || $('body').scrollTop();
  142. if (!anchor) return;
  143. //binary search.
  144. let l = 0, r = anchor.length - 1, mid;
  145. while (l < r) {
  146. mid = (l + r + 1) >> 1;
  147. if (anchor[mid] === scrollTop) l = r = mid;
  148. else if (anchor[mid] < scrollTop) l = mid;
  149. else r = mid - 1;
  150. }
  151. $(liElements).removeClass('active').eq(l).addClass('active');
  152. }
  153. $(window)
  154. .resize(() => {
  155. anchor = getAnchor();
  156. scrollListener();
  157. })
  158. .scroll(() => {
  159. scrollListener()
  160. });
  161. scrollListener();
  162. }
  163. // function getPicture() {
  164. // const $banner = $('.banner');
  165. // if ($banner.length === 0) return;
  166. // const url = ROOT + 'js/lovewallpaper.json';
  167. // $.get(url).done(res => {
  168. // if (res.data.length > 0) {
  169. // const index = Math.floor(Math.random() * res.data.length);
  170. // $banner.css('background-image', 'url(' + res.data[index].big + ')');
  171. // }
  172. // })
  173. // }
  174. // function getHitokoto() {
  175. // const $hitokoto = $('#hitokoto');
  176. // if($hitokoto.length === 0) return;
  177. // const url = 'http://api.hitokoto.us/rand?length=80&encode=jsc&fun=handlerHitokoto';
  178. // $('body').append('<script src="%s"></script>'.replace('%s',url));
  179. // window.handlerHitokoto = (data) => {
  180. // $hitokoto
  181. // .css('color','transparent')
  182. // .text(data.hitokoto)
  183. // if(data.source) $hitokoto.append('<cite> —— %s</cite>'.replace('%s',data.source));
  184. // else if(data.author) $hitokoto.append('<cite> —— %s</cite>'.replace('%s',data.author));
  185. // $hitokoto.css('color','white');
  186. // }
  187. // }
  188. $(function () {
  189. //set header
  190. setHeader();
  191. setHeaderMenu();
  192. setHeaderMenuPhone();
  193. setHeaderSearch();
  194. setWaves();
  195. setScrollReveal();
  196. setTocToggle();
  197. // getHitokoto();
  198. // getPicture();
  199. $(".article .video-container").fitVids();
  200. setTimeout(function () {
  201. $('#loading-bar-wrapper').fadeOut(500);
  202. }, 300);
  203. if (SEARCH_SERVICE === 'google') {
  204. customSearch = new GoogleCustomSearch({
  205. apiKey: GOOGLE_CUSTOM_SEARCH_API_KEY,
  206. engineId: GOOGLE_CUSTOM_SEARCH_ENGINE_ID,
  207. imagePath: "/images/"
  208. });
  209. }
  210. else if (SEARCH_SERVICE === 'algolia') {
  211. customSearch = new AlgoliaSearch({
  212. apiKey: ALGOLIA_API_KEY,
  213. appId: ALGOLIA_APP_ID,
  214. indexName: ALGOLIA_INDEX_NAME,
  215. imagePath: "/images/"
  216. });
  217. }
  218. else if (SEARCH_SERVICE === 'hexo') {
  219. customSearch = new HexoSearch({
  220. imagePath: "/images/"
  221. });
  222. }
  223. else if (SEARCH_SERVICE === 'azure') {
  224. customSearch = new AzureSearch({
  225. serviceName: AZURE_SERVICE_NAME,
  226. indexName: AZURE_INDEX_NAME,
  227. queryKey: AZURE_QUERY_KEY,
  228. imagePath: "/images/"
  229. });
  230. }
  231. else if (SEARCH_SERVICE === 'baidu') {
  232. customSearch = new BaiduSearch({
  233. apiId: BAIDU_API_ID,
  234. imagePath: "/images/"
  235. });
  236. }
  237. });
  238. })(jQuery);