简体   繁体   English

jQuery:无限滚动和后退按钮

[英]jQuery: infinite scroll and the back button

OK so I know this causes problems with everyone, and it's causing problems with me too.好的,所以我知道这会给每个人带来问题,也给我带来问题。 I'm using the infinite scroll plugin on a client's site, in combination with the isotope plugin to load in their products sequentially, the problem is though as they have 1000's of products, anyone browsing the site then clicking into a product, when they click the back button they'll be returned back to the top (or just above the fold of page one), which is causing quite a lot of issues.我在客户网站上使用无限滚动插件,结合同位素插件按顺序加载他们的产品,但问题是,因为他们有 1000 种产品,任何浏览网站的人都点击进入产品,当他们点击时后退按钮它们将返回到顶部(或刚好在第一页的折叠上方),这会导致很多问题。

My markup is as follows below:我的标记如下:

$(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); 

    });

});

Any solutions or suggestions would be massively appreciated!任何解决方案或建议将不胜感激!

You can try scroll-frame .It is a bit old may be the answer for you.你可以试试scroll-frame 。它有点旧可能是你的答案。 Here is a link to an infinite scroll demo using it.这是使用它的无限滚动演示的链接。

scrollFrame will hijack the user's click for elements that match the query selector you pass in and instead of reloading the page it will append a modal-like iframe that sits on top of your viewport and points to the element's href. scrollFrame 将劫持用户对与您传入的查询选择器匹配的元素的点击,而不是重新加载页面,它会附加一个类似模态的 iframe,该 iframe 位于您的视口顶部并指向元素的 href。 It then uses HTML5 history APIs to make the back-button function as expected.然后它使用 HTML5 历史 API 使后退按钮按预期运行。

This can be a bit new solution of such problems.这可能是此类问题的一个新解决方案。 https://github.com/highrisehq/snapback_cache https://github.com/highrisehq/snapback_cache

This is what is say's ...这就是所谓的...

Many apps today have some concept of an infinite scrolling feed: Facebook, Twitter, LinkedIn and many more.今天的许多应用程序都有一些无限滚动提要的概念:Facebook、Twitter、LinkedIn 等等。 Almost all of them suffer from the same problem.几乎所有人都面临同样的问题。 If you click on something in the feed that brings you to a new page, when you hit the back button or try to return to that original feed, your place is lost.如果您单击提要中的某些内容将您带到新页面,当您点击后退按钮或尝试返回原始提要时,您的位置就会丢失。 All the scrolling is gone.所有的滚动都消失了。 At Highrise we had that same problem.在 Highrise,我们也遇到了同样的问题。 So this is the library we use to fix that.所以这是我们用来解决这个问题的库。 We call it our Snapback Cache, and it's made a big improvement to how people can use infinite scroll in our app and still get a lot of work done without losing their place.我们将其称为 Snapback Cache,它对人们如何在我们的应用程序中使用无限滚动并在不失去位置的情况下完成大量工作进行了重大改进。

We solved this problem with a combination of (1) opening the page linked to on the infinite scroll page in a new tab;我们通过以下组合解决了这个问题:(1)在新选项卡中打开无限滚动页面上链接到的页面; (2) using javascript to return to the parent. (2) 使用javascript返回父级。

Since I have seen a lot of comment about the difficulty of returning to the parent, I am posting our code for that below.由于我看到很多关于返回父级的难度的评论,我在下面发布了我们的代码。 The two functions are put into onclick attributes in the anchor tags used for the navigation buttons.这两个函数被放入用于导航按钮的锚标记中的 onclick 属性中。

Using the name of the parent window solves the problem of intervening tabs opened before returning to the parent;使用父窗口的名称解决了在返回父窗口之前插入标签打开的问题; without this, the tab returned to could be the most recently opened tab, rather than the parent.如果没有这个,返回的选项卡可能是最近打开的选项卡,而不是父选项卡。

/**
 * 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