From 923ae1a61b84a5152bc91c9abb2fbc19e1bfd30d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=8B=E6=9F=9A=E6=A2=93?= Date: Sat, 16 May 2020 00:03:41 +0800 Subject: [PATCH] Pjax --- _config.yml | 8 +- layout/_meta/author.ejs | 2 +- layout/_meta/valinecount.ejs | 15 ++ layout/_partial/article.ejs | 8 - layout/_partial/cover.ejs | 31 ++-- layout/_partial/footer.ejs | 1 - layout/_partial/head.ejs | 17 +- layout/_partial/header.ejs | 4 +- layout/_partial/scripts.ejs | 282 +++++++++++++++++++----------- layout/_third-party/clipboard.ejs | 101 ++++++----- layout/_third-party/fancybox.ejs | 2 +- layout/_third-party/pjaxdata.ejs | 78 +++++++++ layout/layout.ejs | 9 +- layout/links.ejs | 1 + source/css/_highlight/index.styl | 1 + source/css/_layout/navbar.styl | 12 +- source/css/_layout/sidebar.styl | 2 +- source/js/app.js | 203 ++++++++++++++------- source/js/search.js | 34 ++-- 19 files changed, 529 insertions(+), 282 deletions(-) create mode 100644 layout/_meta/valinecount.ejs create mode 100644 layout/_third-party/pjaxdata.ejs diff --git a/_config.yml b/_config.yml index 2bf4001..183db64 100755 --- a/_config.yml +++ b/_config.yml @@ -211,6 +211,10 @@ meta: # 文章浏览计数 counter: icon: fas fa-eye + # 文章评论数量:只支持 valine + valinecount: + icon: fal fa-comment-lines + desc: '条评论' # 文章字数和阅读时长 wordcount: icon_wordcount: fas fa-keyboard @@ -487,7 +491,7 @@ comments: valine: appId: dogUA2FSKGTo029M1SEwGROT-MdYXbMMI # your appId appKey: u0NdtQ8nvHoMdJPSYqm1LRxE # your appKey - js: https://cdn.jsdelivr.net/npm/valine@1.4/dist/Valine.min.js + js: path: # All pages use the same path (share the same comments data) meta: nick,mail,link # valine comment header info requiredFields: ['nick','mail'] @@ -574,7 +578,7 @@ comments: search: enable: true service: hexo # hexo, google, algolia, azure, baidu - js: https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2.6.5/js/search.js + # js: https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2.6.4/js/search.js google: apiKey: engineId: diff --git a/layout/_meta/author.ejs b/layout/_meta/author.ejs index 34fca3d..5fd8ce2 100755 --- a/layout/_meta/author.ejs +++ b/layout/_meta/author.ejs @@ -5,7 +5,7 @@ if (post.author == undefined) { %>
- +

<%- post.author.name %>

diff --git a/layout/_meta/valinecount.ejs b/layout/_meta/valinecount.ejs new file mode 100644 index 0000000..744b04a --- /dev/null +++ b/layout/_meta/valinecount.ejs @@ -0,0 +1,15 @@ +<% if (theme.comments.valine && theme.comments.valine.appId){ %> +
+ <% + var path = post.path || page.path; + if (path.length > 9 && (path.substring(path.length - 10) == 'index.html')) { + path = path.substring(0, path.length - 10); + } + %> + + + 0 +  <%- theme.meta.valinecount.desc %> + +
+<% } %> diff --git a/layout/_partial/article.ejs b/layout/_partial/article.ejs index 92d0c67..6d8b98d 100755 --- a/layout/_partial/article.ejs +++ b/layout/_partial/article.ejs @@ -58,11 +58,3 @@ <%- partial('../_third-party/mathjax') %> <% } %> -<% if (page.layout == 'post' && page.title){ %> - -<% } %> diff --git a/layout/_partial/cover.ejs b/layout/_partial/cover.ejs index da9d13f..dfaaae9 100644 --- a/layout/_partial/cover.ejs +++ b/layout/_partial/cover.ejs @@ -1,17 +1,18 @@ -<% if (page.cover == true) { %> -
- <% if (is_home() && page.prev == 0 && theme.cover.scheme == 'docs') { %> - - <%- partial('../_cover/index') %> - - - <% } else { %> - - <%- partial('../_cover/index') %> - - <% } %> - <%- partial('header', null, {cache: false, path: path}) %> -
-<% } else { %> +<% +var enableCover = false; // 封面是否开启 +if(theme.cover && theme.cover.scheme) { + enableCover = true; +} +%> + +<% if (enableCover) { %> +
+ + <%- partial('header', null, {cache: false, path: path}) %> +
+<% } else { %> <%- partial('header', null, {cache: false, path: path}) %> <% } %> diff --git a/layout/_partial/footer.ejs b/layout/_partial/footer.ejs index 6b7d2bc..1fbeee8 100755 --- a/layout/_partial/footer.ejs +++ b/layout/_partial/footer.ejs @@ -46,4 +46,3 @@ <% }) %> <% } %> - diff --git a/layout/_partial/head.ejs b/layout/_partial/head.ejs index fcc1e23..9737ba2 100755 --- a/layout/_partial/head.ejs +++ b/layout/_partial/head.ejs @@ -62,12 +62,17 @@ } } %> - <% if (title) { %><%= title %> - <% } %><%= config.title %> + <% if (title) { %><%= title %> - <% } %><%= config.title %> <% if (keywords){ %> + <% } else { %> + <% } %> + <% if (description){ %> + <% } else { %> + <% } %> @@ -85,13 +90,13 @@ <%- css('https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css') %> - + <% if (theme.plugins.nodewaves && theme.plugins.nodewaves.css) { %> <%- css(theme.plugins.nodewaves.css) %> <% } %> <% if (config.favicon) { %> - + <% } %> <% if (theme.plugins.aplayer.enable) { %> @@ -119,11 +124,7 @@ <%- css('css/style.css') %> <% } %> - + <% if (config.google_analytics_key) { %> diff --git a/layout/_partial/header.ejs b/layout/_partial/header.ejs index 98a91a8..7acb356 100755 --- a/layout/_partial/header.ejs +++ b/layout/_partial/header.ejs @@ -1,5 +1,5 @@
-
+
@@ -119,5 +119,3 @@
- - diff --git a/layout/_partial/scripts.ejs b/layout/_partial/scripts.ejs index 3fcf8dc..81b4ea9 100755 --- a/layout/_partial/scripts.ejs +++ b/layout/_partial/scripts.ejs @@ -1,4 +1,6 @@ <%- js('https://cdn.jsdelivr.net/npm/jquery@3.4/dist/jquery.min.js') %> +<%- js('https://cdn.jsdelivr.net/npm/pjax@0.2.8/pjax.min.js') %> + <% if (theme.search && theme.search.enable) { %> <% } %> + <% if (theme.plugins.instant_page) { %> - -<% } %> -<% if (theme.plugins.scrollreveal && theme.plugins.scrollreveal.js) { %> - - -<% } %> -<% if (theme.plugins.nodewaves && theme.plugins.nodewaves.js) { %> - <%- js({src: theme.plugins.nodewaves.js}) %> - -<% } %> -<% if (theme.plugins.busuanzi) { %> - + <% } %> <% if (theme.plugins.backstretch && theme.plugins.backstretch.enable && (theme.plugins.backstretch.images || page.images)) { %> @@ -105,6 +79,7 @@ <% }) %> <% } %> + <% var enableDisqus = false; var enableLivere = false; @@ -112,7 +87,7 @@ var enableVssue = false; var enableValine = false; var enableMiniValine = false; - if (theme.comments && page && page.comments == true) { + if (theme.comments) { if (theme.comments.disqus && theme.comments.disqus.shortname) { enableDisqus = true; } @@ -132,10 +107,8 @@ enableMiniValine = true; } } - if (theme.comments.valine && theme.comments.valine.appId && theme.comments.valine.visitor && (theme.layout.on_list.meta.indexOf('counter') > -1)) { - enableValine = true; - } %> + <% if (enableDisqus){ %> <% } %> - <% if (enableValine){ %> <% if (theme.comments.valine.js) { %> <%- js(theme.comments.valine.js) %> @@ -214,59 +186,63 @@ <%- js(['js/valine.js']) %> <% } %> <% } %> - <% if (enableMiniValine){ %> <% if (theme.comments.minivaline.js) { %> <%- js(theme.comments.minivaline.js) %> @@ -308,14 +284,68 @@ <% } %> + +<% if (theme.plugins.clipboard && (theme.style.body.highlight.copy_btn == true)) { %> + <%- partial('_third-party/clipboard') %> +<% } %> + + +<% if (theme.plugins.scrollreveal && theme.plugins.scrollreveal.js) { %> + + +<% } %> + + + +<%- partial('_third-party/fancybox') %> + + <% if (config.use_cdn && theme.info.cdn && theme.info.cdn.js) { %> <%- js(theme.info.cdn.js) %> <% } else { %> <%- js(['js/app.js']) %> <% } %> -<% if (theme.search && theme.search.enable && theme.search.js) { %> - <%- js(theme.search.js) %> + +<% if (theme.search && theme.search.enable) { %> + <% if (theme.search.js) { %> + <%- js(theme.search.js) %> + <% } else { %> + <%- js(['js/search.js']) %> + <% } %> +<% } %> + +<% if (theme.plugins.busuanzi) { %> + <% } %> + +<% if (theme.plugins.nodewaves && theme.plugins.nodewaves.js) { %> + <%- js({src: theme.plugins.nodewaves.js}) %> + +<% } %> + <% if (theme.plugins.comment_typing) { %> <%- js(theme.plugins.comment_typing) %> <% } %> @@ -327,17 +357,73 @@ <% } %> <% } %> - - -<% if (theme.plugins.clipboard && (theme.style.body.highlight.copy_btn == true)) { %> - <%- partial('_third-party/clipboard') %> -<% } %> - - - -<%- partial('_third-party/fancybox') %> - <% if (config.import && config.import.script){ %> <% (config.import.script||[]).forEach(function(item){ %> <%- item %><% }) %> <% } %> + +<%- js(['js/message.js']) %> + + \ No newline at end of file diff --git a/layout/_third-party/clipboard.ejs b/layout/_third-party/clipboard.ejs index fce9e37..0cf135f 100644 --- a/layout/_third-party/clipboard.ejs +++ b/layout/_third-party/clipboard.ejs @@ -1,54 +1,53 @@ + + function pjax_initCopyCode() { + var copyHtml = ''; + copyHtml += ''; + $(".highlight .code pre").before(copyHtml); + $(".article pre code").before(copyHtml); + var clipboard = new ClipboardJS('.btn-copy', { + target: function (trigger) { + return trigger.nextElementSibling + } + }); + clipboard.on('success', function (e) { + let $btn = $(e.trigger); + $btn.addClass('copied'); + let $icon = $($btn.find('i')); + $icon.removeClass('fa-copy'); + $icon.addClass('fa-check-circle'); + let $span = $($btn.find('span')); + $span[0].innerText = 'COPIED'; + wait(function () { + $icon.removeClass('fa-check-circle'); + $icon.addClass('fa-copy'); + $span[0].innerText = 'COPY' + }, 2000) + }); + clipboard.on('error', function (e) { + e.clearSelection(); + let $btn = $(e.trigger); + $btn.addClass('copy-failed'); + let $icon = $($btn.find('i')); + $icon.removeClass('fa-copy'); + $icon.addClass('fa-times-circle'); + let $span = $($btn.find('span')); + $span[0].innerText = 'COPY FAILED'; + wait(function () { + $icon.removeClass('fa-times-circle'); + $icon.addClass('fa-copy'); + $span[0].innerText = 'COPY' + }, 2000) + }) + } + $(function () { + pjax_initCopyCode() + }); + \ No newline at end of file diff --git a/layout/_third-party/fancybox.ejs b/layout/_third-party/fancybox.ejs index 7b3711d..8f02f58 100644 --- a/layout/_third-party/fancybox.ejs +++ b/layout/_third-party/fancybox.ejs @@ -10,7 +10,7 @@ } $(element).attr("data-fancybox", "images"); var caption = ""; // 描述信息 - if ($(this).attr('alt')) { // 标准 markdown 描述信息 + if ($(this).attr('alt')) { // 判断当前页面是否存在描述信息 $(element).attr('data-caption', $(this).attr('alt')); caption = $(this).attr('alt'); } diff --git a/layout/_third-party/pjaxdata.ejs b/layout/_third-party/pjaxdata.ejs new file mode 100644 index 0000000..d7bc949 --- /dev/null +++ b/layout/_third-party/pjaxdata.ejs @@ -0,0 +1,78 @@ + + + +<% +var changeValinePath = 'none'; // 自定义的 valine 评论路径 +var changeValinePlaceholder = 'none'; // 自定义的 valine 评论描述 +var ispage = false; // 一二级导航栏切换控制 +var postTitle = ''; // 当前文章标题,用于二级导航栏赋值 +var enableCover = false; // 封面是否开启 +var frontMatterCover = 'none'; // 封面控制 + +var enableValine = false; +if (theme.comments && page && page.comments == true && theme.comments.valine && theme.comments.valine.appId) { + enableValine = true; +} + +if (page && page.comments == true) { + if (enableValine && page.valine && page.valine.path) { + changeValinePath = page.valine.path; + } + if (enableValine && page.valine && page.valine.placeholder) { + changeValinePlaceholder = page.valine.placeholder; + } +} + +if(theme.cover && theme.cover.scheme) { + enableCover = true; +} + +if (enableCover && page && page.cover) { + frontMatterCover = page.cover; + if (is_home() && page.prev == 0 && theme.cover.scheme == 'docs') { + frontMatterCover = 'docs'; + } else { + frontMatterCover = 'blog'; + } +} + +if (page && page.layout == 'post' && page.title) { + ispage = true; + postTitle = page.title; +} + +%> + + + +<% if (enableCover) { %> + +<% } %> \ No newline at end of file diff --git a/layout/layout.ejs b/layout/layout.ejs index b8ad40f..7769373 100755 --- a/layout/layout.ejs +++ b/layout/layout.ejs @@ -4,13 +4,14 @@ <%- partial('_partial/cover') %>
-
+
+ <%- partial('_third-party/pjaxdata') %> <%- body %> -
- <%- partial('_partial/scripts') %> - +
+ <%- partial('_partial/scripts') %> +
diff --git a/layout/links.ejs b/layout/links.ejs index fd4d54d..83ea3db 100755 --- a/layout/links.ejs +++ b/layout/links.ejs @@ -54,6 +54,7 @@

<%- page.more %> + <%- partial('_partial/meta',{post:page, position:'footer'}) %> <%- partial('_third-party/comments') %> diff --git a/source/css/_highlight/index.styl b/source/css/_highlight/index.styl index e1a1e89..2b0bf0b 100644 --- a/source/css/_highlight/index.styl +++ b/source/css/_highlight/index.styl @@ -115,6 +115,7 @@ pre margin-left: 0 position: sticky left: 0 + z-index: 1 background-color: darken($color-codeblock, 5) pre color: $color-meta diff --git a/source/css/_layout/navbar.styl b/source/css/_layout/navbar.styl index 1a4f6ce..371736d 100644 --- a/source/css/_layout/navbar.styl +++ b/source/css/_layout/navbar.styl @@ -1,19 +1,10 @@ #loading-bar-wrapper position: fixed + width: 100% top: 0 - left: 0 height: 2px - width: 100% z-index: 99999 -#loading-bar - position: fixed - width: 0 - height: 2px - trans() - background-color: $color-theme - - .l_header $iconW = 32px $iconMargin = 4px @@ -260,7 +251,6 @@ ul.list-v display: block color: mix($color-text, $color-card, 90) font-size: $fontsize-meta - font-weight: bold line-height: 33px padding: 0 $gap + $border-line + 2px 0 $gap height: 32px diff --git a/source/css/_layout/sidebar.styl b/source/css/_layout/sidebar.styl index fff8f91..dd9881e 100644 --- a/source/css/_layout/sidebar.styl +++ b/source/css/_layout/sidebar.styl @@ -48,8 +48,8 @@ header border-top-left-radius: $border-card border-top-right-radius: $border-card - font-weight: bold padding: "calc(%s - 2px)" % $gap + font-weight: bold padding-bottom: 0 &,a color: $color-theme diff --git a/source/js/app.js b/source/js/app.js index 0460bc3..6855470 100755 --- a/source/js/app.js +++ b/source/js/app.js @@ -3,12 +3,24 @@ var customSearch; (function ($) { "use strict"; + + // 校正页面定位(被导航栏挡住的区域) var scrollCorrection = 80; // (header height = 64px) + (gap = 16px) - const $headerAnchor = $('.l_header', '.cover-wrapper'); + var $headerAnchor = $('.l_header', '.cover-wrapper'); if ($headerAnchor[0]) { scrollCorrection = $headerAnchor[0].clientHeight + 16; } + // 尝试: 重设数据值 + function restData() { + scrollCorrection = 80; + $headerAnchor = $('.l_header', '.cover-wrapper'); + if ($headerAnchor[0]) { + scrollCorrection = $headerAnchor[0].clientHeight + 16; + } + } + + // 校正页面定位(被导航栏挡住的区域) function scrolltoElement(elem, correction = scrollCorrection) { const $elem = elem.href ? $(elem.getAttribute('href')) : $(elem); $('html, body').animate({ @@ -16,19 +28,20 @@ var customSearch; }, 500); } + // 设置滚动锚点 function setScrollAnchor() { - // button - const $postsBtn = $('.menu .active'); - const $topBtn = $('.s-top'); - const $titleBtn = $('h1.title', '#header-meta'); - // anchor - const $bodyAnchor = $('.l_body'); - // action + const $postsBtn = $('.menu .active'); // 一级导航上的当前激活的按钮 + const $topBtn = $('.s-top'); // 向上 + const $titleBtn = $('h1.title', '#header-meta'); // 文章内标题 + const $bodyAnchor = $('.l_body'); // 页面主体 + if ($postsBtn.length && $bodyAnchor) { $postsBtn.click(e => { e.preventDefault(); e.stopPropagation(); - scrolltoElement($bodyAnchor); + if($postsBtn.attr("href") != "/") // TODO: fix it + scrolltoElement($bodyAnchor); + e.stopImmediatePropagation(); }); } if ($titleBtn.length && $bodyAnchor) { @@ -36,6 +49,7 @@ var customSearch; e.preventDefault(); e.stopPropagation(); scrolltoElement($bodyAnchor); + e.stopImmediatePropagation(); }); } if ($topBtn.length && $bodyAnchor) { @@ -43,17 +57,32 @@ var customSearch; e.preventDefault(); e.stopPropagation(); scrolltoElement($bodyAnchor); + e.stopImmediatePropagation(); }); } + //========================================== + const $coverAnchor = $('.cover-wrapper'); + + var enableCover = $('#pjax-enable-cover').text(); // Pjax 处理 + var showHeaderPoint = 0; if ($coverAnchor[0]) { - showHeaderPoint = $coverAnchor[0].clientHeight - 180; + if(enableCover == "true" && $('.cover.half').css('display') !== 'none') // Pjax 处理 + showHeaderPoint = $coverAnchor[0].clientHeight - 180; } + var pos = document.body.scrollTop; + if(enableCover == "true" && $('.cover.half').css('display') === 'none') + pos += 180; // Pjax 处理 + $(document, window).scroll(() => { - const scrollTop = $(window).scrollTop(); + let scrollTop = $(window).scrollTop(); // 滚动条距离顶部的距离 + + if(enableCover == "true" && $('.cover.half').css('display') === 'none') + scrollTop += 180; // Pjax 处理 + const del = scrollTop - pos; pos = scrollTop; if (scrollTop > 180) { @@ -72,15 +101,27 @@ var customSearch; $headerAnchor.removeClass('show'); } }); + //========================================== } + // 设置导航栏 function setHeader() { + var HEXO_ISPAGE = $.trim($('#pjax-ispage').text()); + if(HEXO_ISPAGE == 'true') + window.subData = { + title: $.trim($('#pjax-pageTitle').text()), + tools: true + } + if (!window.subData) return; - const $wrapper = $('header .wrapper'); - const $comment = $('.s-comment', $wrapper); - const $toc = $('.s-toc', $wrapper); + const $wrapper = $('header .wrapper'); // 整个导航栏 + const $comment = $('.s-comment', $wrapper); // 评论按钮 桌面端 移动端 + const $toc = $('.s-toc', $wrapper); // 目录按钮 仅移动端 + + $comment.show(); // 显示 (某些文章可能关闭了评论,故先行显示) + $wrapper.find('.nav-sub .title').text(window.subData.title); // 二级导航文章标题 - $wrapper.find('.nav-sub .title').text(window.subData.title); + // 决定一二级导航栏的切换 let pos = document.body.scrollTop; $(document, window).scroll(() => { const scrollTop = $(window).scrollTop(); @@ -90,21 +131,22 @@ var customSearch; $wrapper.addClass('sub'); } else if (del <= -50) { pos = scrollTop; - $wrapper.removeClass('sub'); + $wrapper.removeClass('sub'); // <---- 取消二级导航显示 } }); // bind events to every btn - const $commentTarget = $('.l_body .comments'); + let $commentTarget = $('.l_body .comments'); // 评论区域 if ($commentTarget.length) { - $comment.click(e => { + $comment.click(e => { // 评论按钮点击后 跳转到评论区域 e.preventDefault(); e.stopPropagation(); - scrolltoElement($commentTarget); + scrolltoElement($('.l_body .comments')); + e.stopImmediatePropagation(); }); - } else $comment.remove(); + } else $comment.hide(); // 关闭了评论,则隐藏 - const $tocTarget = $('.l_body .toc-wrapper'); + const $tocTarget = $('.l_body .toc-wrapper'); // 侧边栏的目录列表 PC if ($tocTarget.length && $tocTarget.children().length) { $toc.click((e) => { e.stopPropagation(); @@ -112,6 +154,7 @@ var customSearch; $toc.toggleClass('active'); }); $(document).click(function (e) { + e.stopPropagation(); $tocTarget.removeClass('active'); $toc.removeClass('active'); }); @@ -120,11 +163,9 @@ var customSearch; $toc.removeClass('active'); }); } else $toc.remove(); - - - } + // 设置导航栏菜单选中状态 function setHeaderMenuSelection() { var $headerMenu = $('body .navigation'); // 先把已经激活的取消激活 @@ -159,39 +200,23 @@ var customSearch; } } - function setHeaderMenuPhone() { - // var $switcher = $('.l_header .switcher .s-menu'); - // var $menu = $('body .menu-phone'); - // $switcher.click(function (e) { - // e.stopPropagation(); - // $menu.toggleClass('show'); - // $switcher.toggleClass('active'); - // }); - // $(document).click(function (e) { - // // $menu.removeClass('show'); - // $switcher.removeClass('active'); - // }); - // $(document, window).scroll(() => { - // $menu.removeClass('show'); - // $switcher.removeClass('active'); - // }); - } - + // 设置导航栏搜索框 fix √ function setHeaderSearch() { - var $switcher = $('.l_header .switcher .s-search'); - var $header = $('.l_header'); - var $search = $('.l_header .m_search'); + var $switcher = $('.l_header .switcher .s-search'); // 搜索按钮 移动端 + var $header = $('.l_header'); // 移动端导航栏 + var $search = $('.l_header .m_search'); // 搜索框 桌面端 if ($switcher.length === 0) return; $switcher.click(function (e) { e.stopPropagation(); - $header.toggleClass('z_search-open'); + $header.toggleClass('z_search-open'); // 激活移动端搜索框 + $switcher.toggleClass('active'); // 搜索按钮 $search.find('input').focus(); - $switcher.toggleClass('active'); }); $(document).click(function (e) { $header.removeClass('z_search-open'); $switcher.removeClass('active'); }); + $search.click(function (e) { e.stopPropagation(); }); @@ -224,12 +249,13 @@ var customSearch; }); } + // 设置导航栏搜索框 function setTocToggle() { - const $toc = $('.toc-wrapper'); + const $toc = $('.toc-wrapper'); // 侧边栏 TOC 移动端 if ($toc.length === 0) return; $toc.click((e) => { - e.stopPropagation(); - $toc.addClass('active'); + e.stopPropagation(); + $toc.addClass('active'); }); $(document).click(() => $toc.removeClass('active')); @@ -248,13 +274,19 @@ var customSearch; } }); - const liElements = Array.from($toc.find('li a')); + let liElements = Array.from($toc.find('li a')); //function animate above will convert float to int. - const getAnchor = () => liElements.map(elem => Math.floor($(elem.getAttribute('href')).offset().top - scrollCorrection)); + let getAnchor = () => liElements.map(elem => Math.floor($(elem.getAttribute('href')).offset().top - scrollCorrection)); let anchor = getAnchor(); - const scrollListener = () => { - const scrollTop = $('html').scrollTop() || $('body').scrollTop(); + let domHeigth = $(document).height(); + let scrollListener = () => { + let scrollTop = $('html').scrollTop() || $('body').scrollTop(); + if ($(document).height() != domHeigth) { // dom 高度发生变化: 普遍来说,是图片懒加载造成的 + scrollTop = $('html').scrollTop() || $('body').scrollTop(); + domHeigth = $(document).height(); + anchor = getAnchor(); + } if (!anchor) return; //binary search. let l = 0, @@ -267,18 +299,26 @@ var customSearch; else r = mid - 1; } $(liElements).removeClass('active').eq(l).addClass('active'); - } - $(window) - .resize(() => { + }; + + $(window).scroll(() => { + scrollListener(); + }); + + // 监听窗口改变事件 + let resizeTimer = null; + $(window).bind('resize', function (){ + if (resizeTimer) clearTimeout(resizeTimer); + resizeTimer = setTimeout(function(){ anchor = getAnchor(); scrollListener(); - }) - .scroll(() => { - scrollListener() - }); + } , 100); + }); + scrollListener(); } + // 设置搜索服务 function setSearchService() { if (SEARCH_SERVICE === 'google') { customSearch = new GoogleCustomSearch({ @@ -312,6 +352,7 @@ var customSearch; } } + // 设置 tabs 标签 function setTabs() { const $tabs = $('.tabs'); if ($tabs.length === 0) return; @@ -336,19 +377,45 @@ var customSearch; $(function () { setHeader(); setHeaderMenuSelection(); - setHeaderMenuPhone(); setHeaderSearch(); setTocToggle(); setScrollAnchor(); setSearchService(); setTabs(); - // $(".article .video-container").fitVids(); + + // 全屏封面底部箭头 $('.scroll-down').on('click', function () { - scrolltoElement('.l_body'); - }); - setTimeout(function () { - $('#loading-bar-wrapper').fadeOut(500); - }, 300); + scrolltoElement('.l_body'); + }); + + + // addEventListener是先绑定先执行,此处的绑定后执行 + document.addEventListener('pjax:complete', function () { + try { + $(function () { + restData(); + setHeader(); + setHeaderMenuSelection(); + setTocToggle(); + setScrollAnchor(); + setTabs(); + }); + } catch (error) { + console.log(error); + } + }); }); + })(jQuery); + +/*Valine Admin*/ +if(window.location.hash){ + var checkExist = setInterval(function() { + if (typeof jQuery == 'undefined'){return;} + if ($("#"+window.location.hash.split("#")[1]).length) { + $('html, body').animate({scrollTop: $("#"+window.location.hash.split("#")[1]).offset().top-90}, 1000); + clearInterval(checkExist); + } + }, 100); +} \ No newline at end of file diff --git a/source/js/search.js b/source/js/search.js index 5e671b4..d0b3d6e 100755 --- a/source/js/search.js +++ b/source/js/search.js @@ -122,19 +122,29 @@ var SearchService = ""; } }; + self.getUrlRelativePath = function (url) { + var arrUrl = url.split("//"); + var start = arrUrl[1].indexOf("/"); + var relUrl = arrUrl[1].substring(start); + if (relUrl.indexOf("?") != -1) { + relUrl = relUrl.split("?")[0]; + } + return relUrl; + } + /** * Generate html for one result * @param url : (string) url * @param title : (string) title * @param digest : (string) digest - * @param index : 标号 */ - self.buildResult = function(url, title, digest, index) { + self.buildResult = function (url, title, digest) { + var result = self.getUrlRelativePath(url); var html = ""; html = "
  • "; - html += ""; - html += "" +title+ ""; - if (digest !== "") html += "" +digest+ ""; + html += ""; + html += "" + title + ""; + if (digest !== "") html += "" + digest + ""; html += ""; html += "
  • "; return html; @@ -233,7 +243,7 @@ var SearchService = ""; self.init(); }; - var template = ''; + var template = ''; })(jQuery); var AlgoliaSearch; @@ -265,6 +275,7 @@ var AlgoliaSearch; var digest = ""; html += self.buildResult(url, title, digest, index+1); }); + html += ""; return html; }; @@ -366,6 +377,7 @@ var AzureSearch; var digest = row.excerpt || ""; html += self.buildResult(url, title, digest); }); + html += ""; return html; }; @@ -473,6 +485,7 @@ var BaiduSearch; if (self.contentSearch(post, queryText)) html += self.buildResult(post.linkUrl, post.title, post.abstract); }); + html += ""; return html; }; @@ -567,6 +580,7 @@ var GoogleCustomSearch = ""; var digest = (row.htmlSnippet || "").replace('
    ',''); html += self.buildResult(url, title, digest); }); + html += ""; return html; }; @@ -710,12 +724,12 @@ var HexoSearch; */ self.buildResultList = function(data, queryText) { var results = [], - html = ""; - var i = 1; - $.each(data, function(index, post) { + html = ""; + $.each(data, function (index, post) { if (self.contentSearch(post, queryText)) - html += self.buildResult(post.permalink, post.title, post.digest, i++); + html += self.buildResult(post.permalink, post.title, post.digest); }); + html += ""; return html; };