简体   繁体   English

返回上一页时无限滚动中的jQuery滚动位置历史记录

[英]jQuery scroll position history in infinite scroll when go back to previous page

Suppose I scrolling down to the 18/50 of my <div> on my Index page then I go to another page in the same tab and go back to previous page.假设我向下滚动到索引页面上<div>的 18/50,然后我转到同一选项卡中的另一个页面并返回上一页。

How do I make a scroll down to the same position on 18/50 of my <div> ?如何在<div> 18/50 处向下滚动到相同位置?

scroll.js滚动.js

var ajax_arry = [];
var ajax_index = 0;
var sctp = 100;
$(function () {
    $('#loading').show();
    $.ajax({
        url: "scroll.php",
        type: "POST",
        data: "actionfunction=showData&page=1",
        cache: false,
        success: function (response) {
            $('#loading').hide();
            $('#demoajax').html(response);

        }

    });
    $(window).scroll(function () {

        var height = $('#demoajax').height();
        var scroll_top = $(this).scrollTop();
        if (ajax_arry.length > 0) {
            $('#loading').hide();
            for (var i = 0; i < ajax_arry.length; i++) {
                ajax_arry[i].abort();
            }
        }
        var page = $('#demoajax').find('.nextpage').val();
        var isload = $('#demoajax').find('.isload').val();

        if ((($(window).scrollTop() + document.body.clientHeight) == $(window).height()) && isload == 'true') {
            $('#loading').show();
            var ajaxreq = $.ajax({
                url: "scroll.php",
                type: "POST",
                data: "actionfunction=showData&page=" + page,
                cache: false,
                success: function (response) {
                    $('#demoajax').find('.nextpage').remove();
                    $('#demoajax').find('.isload').remove();
                    $('#loading').hide();

                    $('#demoajax').append(response);

                }

            });
            ajax_arry[ajax_index++] = ajaxreq;

        }
        return false;

        if ($(window).scrollTop() == $(window).height()) {
            alert("bottom!");
        }
    });

});

store the window offset top in localstorage将窗口偏移量 top 存储在 localstorage 中

localStorage.setItem('windowscrolltop',$(window).scrollTop());

and when u came back to the page and binding done animate to that point.当你回到页面并绑定完成时,动画到那个点。

var windowscrolltop = localStorage.getItem('windowscrolltop');
  $('html, body').animate({
    scrollTop: windowscrolltop
}, 500);

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

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