简体   繁体   English

在 WordPress 中使用 Infinite Scroll 更改 URL(使用 pushState)

[英]Change URL (with pushState) using Infinite Scroll in WordPress

I've set up the infinite scroll from here and it works: https://github.com/pklauzinski/jscroll .我已经从这里设置了无限滚动,它可以工作: https : //github.com/pklauzinski/jscroll Additionally I would like to change the URL in the browser address bar but I can't get it working.另外,我想更改浏览器地址栏中的 URL,但无法正常工作。 There is a similar solution on https://github.com/wataruoguchi/clever-infinite-scroll but I would like to load only the next post always and not all the posts called by the selector. https://github.com/wataruoguchi/clever-infinite-scroll上有一个类似的解决方案,但我想始终只加载下一篇文章,而不是选择器调用的所有文章。

How could I use pushState with jScroll?我如何将 pushState 与 jScroll 一起使用? The goal is to use jScroll and the URL needs to be changed when scrolling to previous or next posts.目标是使用 jScroll 并且在滚动到上一个或下一个帖子时需要更改 URL。

You can use the callback parameter of jscroll in order to fire a function after it finished to load the content.您可以使用 jscroll 的callback参数来在完成加载内容后触发函数。

var currentPage = 1;
$('.jscroll').jscroll({
    // your current jsscroll options
    callback: function() {
        currentPage++;
        History.pushState(null, $('head > title').html(), 'http://yourpageurl.com/page/' + currentPage);
    }
});

In that callback you can use pushState to manipulate the browser history.在该回调中,您可以使用pushState来操作浏览器历史记录。 Replace the url with the correct one, and update currentPage if your infinitescroll is supposed to be going backward too.将 url 替换为正确的 url,如果您的无限滚动也应该向后,则更新 currentPage。

You'll need to scroll to the specific position on load too, from the page parameter.您还需要从页面参数滚动到加载时的特定位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM