Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem occur when using Pjax/Pjax函数重载问题 #649

Closed
2 tasks done
ojhdt opened this issue Mar 23, 2020 · 8 comments
Closed
2 tasks done

Problem occur when using Pjax/Pjax函数重载问题 #649

ojhdt opened this issue Mar 23, 2020 · 8 comments
Labels
discussion Any questions about this theme. wontfix Something that will not be done or fixed.

Comments

@ojhdt
Copy link

ojhdt commented Mar 23, 2020

Please make sure you took care of the following things before you submit this issue. Thank you!

我尝试为Icarus添加Pjax,在启动成功后出现以下问题:

  • LightGallery失效
  • back-to-top 按键移位到页面左下角
  • 点击toc时页面滚动动画丢失

希望得到解决方案,提供可用重载方法及编辑位置。

@ppoffice
Copy link
Owner

请描述你添加pjax所做的修改

@ojhdt
Copy link
Author

ojhdt commented Mar 23, 2020

请描述你添加pjax所做的修改

  1. <head>内引入Nprogress,Jquery与Pjax
<head>
...
<!-- require Nprogress -->
<script src='https://unpkg.com/[email protected]/nprogress.js'></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/nprogress.cssnprogress.css"/>
<!-- require Jquery -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<!-- require Pjax -->
<script src="//cdn.bootcss.com/jquery.pjax/1.9.6/jquery.pjax.min.js"></script>
...
</head>
  1. </body>前引入Pjax启动函数
...
<script>
$(document).pjax('a[target!=_blank]', '#pjax-container', {
    container: '#pjax-container',
    fragment: '#pjax-container',
    timeout: 8000
}).on('pjax:send',
function() {
    NProgress.start();
}).on('pjax:complete',
function() {
    NProgress.done();
});
</script>
</body>
</html>
  1. layout.ejs内一个 container 加入id="pjax-container"用作刷新区域
    <%- _partial('common/navbar') %>
    <% function main_column_class() {
        switch (column_count()) {
            case 1:
                return 'is-12';
            case 2:
                return 'is-8-tablet is-8-desktop is-8-widescreen';
            case 3:
                return 'is-8-tablet is-8-desktop is-6-widescreen'
        }
        return '';
    } %>
    <section class="section">
        <div class="container" id="pjax-container">
            <div class="columns">
                <div class="column <%= main_column_class() %> has-order-2 column-main"><%- body %></div>
                <%- _partial('common/widget', { position: 'left' }) %>
                <%- _partial('common/widget', { position: 'right' }) %>
            </div>
        </div>
    </section>
    <%- _partial('common/footer') %>
    <%- _partial('common/scripts') %>

    <% if (has_config('search.type')) { %>
    <%- _partial('search/' + get_config('search.type')) %>
    <% } %>

@ppoffice
Copy link
Owner

ppoffice commented Mar 23, 2020

@ojhdt 下面是一个简单的patch用来修复你提到的前两点问题,核心思想是在pjax每次加载结束后重新加载主题所需的JavaScript脚本。如果你发现有其他JavaScript问题,也可以举一反三的修改。至于点击toc没有滚动动画,好像Icarus本身也没有滚动动画这个功能。最后主题中已经提供jQuery 3.x了,你不用再引入2.x版本的jQuery。你可以把pjax放到它后面。

<%- _js(cdn('jquery', '3.3.1', 'dist/jquery.min.js')) %>

diff --git a/source/js/back-to-top.js b/source/js/back-to-top.js
index 8cf8cc4..b3d3ca9 100644
--- a/source/js/back-to-top.js
+++ b/source/js/back-to-top.js
@@ -1,4 +1,4 @@
-$(document).ready(function () {
+function loadBackToTop() {
     var $button = $('#back-to-top');
     var $footer = $('footer.footer');
     var $mainColumn = $('.column-main');
@@ -153,4 +153,6 @@ $(document).ready(function () {
     $('#back-to-top').on('click', function () {
         $('body, html').animate({ scrollTop: 0 }, 400);
     });
-});
\ No newline at end of file
+}
+$(document).ready(loadBackToTop);
+$(document).on('pjax:end', loadBackToTop);
\ No newline at end of file
diff --git a/source/js/gallery.js b/source/js/gallery.js
index 5747842..5979549 100644
--- a/source/js/gallery.js
+++ b/source/js/gallery.js
@@ -1,4 +1,4 @@
-document.addEventListener('DOMContentLoaded', function () {
+function loadGallery() {
     if (typeof ($.fn.lightGallery) === 'function') {
         $('.article').lightGallery({ selector: '.gallery-item' });
     }
@@ -8,4 +8,6 @@ document.addEventListener('DOMContentLoaded', function () {
         }
         $('.justified-gallery').justifiedGallery();
     }
-});
\ No newline at end of file
+}
+document.addEventListener('DOMContentLoaded', loadGallery);
+$(document).on('pjax:end', function () { setTimeout(loadGallery) });
\ No newline at end of file
diff --git a/source/js/main.js b/source/js/main.js
index 6b36fd9..ad3fa47 100644
--- a/source/js/main.js
+++ b/source/js/main.js
@@ -1,4 +1,4 @@
-(function ($) {
+function loadMain($) {
     $('.article img:not(".not-gallery-item")').each(function () {
         // wrap images with link and add caption if possible
         if ($(this).parent('a').length === 0) {
@@ -186,4 +186,6 @@
             link.target = '_blank';
         });
     }
-})(jQuery);
+}
+loadMain(jQuery);
+$(document).on('pjax:end', function() { loadMain(jQuery) });

@ppoffice ppoffice added the discussion Any questions about this theme. label Mar 23, 2020
@ojhdt
Copy link
Author

ojhdt commented Mar 24, 2020

@ppoffice
感谢你提供的代码
我发现在引入Aplayer后,点击toc就出现了滚动动画,这使得我认为是Icarus本身的功能。效果可以查看鄙站
该处使用的Aplayer修复了toc点击中文无法跳转的问题。

@ojhdt
Copy link
Author

ojhdt commented Mar 26, 2020

@ppoffice 很抱歉再次打扰您
BackToTop工作的很好,但LightGallery仍然存在问题,具体体现在:

  • 在完整刷新网页时,点击图片后会直接打开图片url
  • Pjax部分刷新时,彻底失效,即图片无法点击,无法放大

另外,在Pjax下的Valine评论读取存在滞后,出现 读取上一篇浏览过的文章的评论 的情况。

@stale
Copy link

stale bot commented Apr 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

1 similar comment
@stale
Copy link

stale bot commented Apr 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix Something that will not be done or fixed. label Apr 15, 2020
@stale stale bot closed this as completed Apr 22, 2020
PJ-568 added a commit to PJ-568/hexo-theme-icarus that referenced this issue Apr 17, 2024
PJ-568 added a commit to PJ-568/hexo-theme-icarus that referenced this issue Apr 25, 2024
PJ-568 added a commit to PJ-568/hexo-theme-icarus that referenced this issue Apr 25, 2024
PJ-568 added a commit to PJ-568/hexo-theme-icarus that referenced this issue Apr 25, 2024
PJ-568 added a commit to PJ-568/hexo-theme-icarus that referenced this issue Apr 25, 2024
PJ-568 added a commit to PJ-568/hexo-theme-icarus that referenced this issue Apr 25, 2024
@PJ-568
Copy link
Contributor

PJ-568 commented May 1, 2024

已并入主线:#1287

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Any questions about this theme. wontfix Something that will not be done or fixed.
Projects
None yet
Development

No branches or pull requests

3 participants