繁体   English   中英

jQuery:无限滚动和后退按钮

[英]jQuery: infinite scroll and the back button

好的,所以我知道这会给每个人带来问题,也给我带来问题。 我在客户网站上使用无限滚动插件,结合同位素插件按顺序加载他们的产品,但问题是,因为他们有 1000 种产品,任何浏览网站的人都点击进入产品,当他们点击时后退按钮它们将返回到顶部(或刚好在第一页的折叠上方),这会导致很多问题。

我的标记如下:

$(window).load(function () {

    var $container = $('.products-grid-wrap');

    $container.imagesLoaded(function () {
        $container.isotope({
            itemSelector: '.products-grid-block',
            filter: '*:not(.hidden), .products-grid-block',
            animationEngine: 'best-available',
            layoutMode: "perfectMasonry",
            perfectMasonry: {
              columnWidth: 280,
              rowHeight: 310
            }
        });         

        $container.infinitescroll({
            navSelector: '#page_nav', // selector for the paged navigation 
            nextSelector: '#page_nav a', // selector for the NEXT link (to page 2)
            itemSelector: '.regular-product-block, .products-grid-block', // selector for all items you'll retrieve
            pixelsFromNavToBottom: Math.round($(window).height() * 1.5),
            bufferPx: Math.round($(window).height() * 1.5),
            loading: {
                finishedMsg: 'No more products to load.',
                img: 'http://www.by-form.net/wp-content/uploads/2014/11/ajax-loader-big.gif'
            }
        },
        // call Isotope as a callback
        function (newElements) {
            var $newElems = $(newElements);
            $newElems.imagesLoaded(function () {
                $container.isotope('insert', $newElems);
                $('.products-grid-rollover-block').hide();                 
                   if(newElements.length > 0){
                       setTimeout(function () {
                            $container.infinitescroll('retrieve');
                            $('.products-grid-wrap').isotope('reLayout');
                            //$('.products-grid-wrap').isotope({
                            //sortBy: 'category',
                                //sortAscending: false });
                        }, 1000);
                   }

            });
        }); 

        setTimeout(function () {
            $container.infinitescroll('retrieve');
        }, 3000); 

    });

});

任何解决方案或建议将不胜感激!

你可以试试scroll-frame 。它有点旧可能是你的答案。 这是使用它的无限滚动演示的链接。

scrollFrame 将劫持用户对与您传入的查询选择器匹配的元素的点击,而不是重新加载页面,它会附加一个类似模态的 iframe,该 iframe 位于您的视口顶部并指向元素的 href。 然后它使用 HTML5 历史 API 使后退按钮按预期运行。

这可能是此类问题的一个新解决方案。 https://github.com/highrisehq/snapback_cache

这就是所谓的...

今天的许多应用程序都有一些无限滚动提要的概念:Facebook、Twitter、LinkedIn 等等。 几乎所有人都面临同样的问题。 如果您单击提要中的某些内容将您带到新页面,当您点击后退按钮或尝试返回原始提要时,您的位置就会丢失。 所有的滚动都消失了。 在 Highrise,我们也遇到了同样的问题。 所以这是我们用来解决这个问题的库。 我们将其称为 Snapback Cache,它对人们如何在我们的应用程序中使用无限滚动并在不失去位置的情况下完成大量工作进行了重大改进。

我们通过以下组合解决了这个问题:(1)在新选项卡中打开无限滚动页面上链接到的页面; (2) 使用javascript返回父级。

由于我看到很多关于返回父级的难度的评论,我在下面发布了我们的代码。 这两个函数被放入用于导航按钮的锚标记中的 onclick 属性中。

使用父窗口的名称解决了在返回父窗口之前插入标签打开的问题; 如果没有这个,返回的选项卡可能是最近打开的选项卡,而不是父选项卡。

/**
 * open url in separate tab
 * saves name of parent window for easy return
 * @param url
 */
function gotoTab(url)
{
    window.name = 'parent';
    window.open(url);
}

/**
 * uses name of parent
 */
function returnToParent()
{
    var goBack = window.open('', window.opener.name);
    window.top.close();
    goBack.focus();
}

暂无
暂无

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

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