0

我想使用 ajax 回调函数进行自动加载,但GetData首先使用相同的参数调用该函数。这是我的javascript代码:

        var currentPage = 0;
        var isFinished = false;
        var lastScrollTop = 0;
     $(window).data('ajaxready', true).scroll(function (e) {
            if ($(window).data('ajaxready') == false) return;


            $(window).scroll(function (event) {
                var st = $(this).scrollTop();
                if (st > lastScrollTop) {
                    if (st > window.innerHeight) {
                        var amountValue = $("#amount").val();

                        var firstPrice = 0;
                        var lastPrice = 10000;

                        InfiniteScroll(firstPrice, lastPrice);
                    }

                } 

            });

        });



 function InfiniteScroll(firstPrice, lastPrice) {

            if (firstPrice < 0 || firstPrice == undefined) {
                firstPrice = 0;
            }
            if (lastPrice < 0 || lastPrice == undefined) {
                lastPrice = 10000;
            }

            if (isFinished) {
                return;
            }


            $('#divPostsLoader').html('<img src="images/loader.gif">');

            var rawPath = window.location.pathname.split('/');
            var categoryId = rawPath[rawPath.length - 1];

            $("#load").show();



            $.ajax({
                type: "POST",
                url: "http://localhost:60579/AjaxCallPage.aspx/GetData",
                data: "{categoryId:" + categoryId +
                            ",page:" + currentPage +
                            ",skip:'9',firstPrice:" + firstPrice + ",lastPrice:" + lastPrice + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (data.d.length < 3) {
                        isFinished = true;
                    }
                    var products = JSON.parse(data.d);
                    $.each(JSON.parse(data.d), function () {

                       // ... do smth..



                    });


                    currentPage += 1;

                    $("#load").hide();
                }
            });
        };

我不向下滚动,但它GetData在第一次调用后调用函数。你有建议吗?

4

1 回答 1

0

问题在这里:

if (st > lastScrollTop) {
                if (st > window.innerHeight) {

检查一下,因为出于某种原因,在没有滚动的情况下进入了这部分功能。如果我有时间我会看看。

于 2013-05-13T06:15:02.893 回答