0

我在滚动上调用一个函数,当我达到我的目的时,我正在取消绑定滚动。但是在单击菜单选项卡时我想再次绑定滚动。我们不能在取消绑定后重新绑定滚动而不刷新页面。这是我的代码。

var currentPage = 1;
var xhr = null;
var flag = 0;
(window).bind('scroll');

$(document).ready(function()
{
 $('#gallery').bind("click",function()
{
 flag = 0;
 currentPage = 1;
 alert(currentPage);
 scroll()
});

});

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

function refresh(){flag = 1; alert("flag");}

function checkScroll() {


if (flag==1){ $(window).unbind('scroll');}




if(nearBottomOfPage() == 0)
{

    currentPage ++;

  xhr = $.ajax(
    {
        url : '/ideabank?page=' + currentPage,
        beforeSend: function() {
                $('#loading').show()
            },
            complete: function(){
                $('#loading').hide()
            },
        success : function(){}
       } );
    }

 }


 function nearBottomOfPage() {
  return scrollDistanceFromBottom();
}

 function scrollDistanceFromBottom(argument) {
 return $(document).height() - ($(window).height() + $(window).scrollTop());
}

  $(window).bind('scroll',function (){
    checkScroll();
 });
4

1 回答 1

0

您可以随时重新绑定滚动事件的处理程序,但是您的三个调用中.bind('scroll')有两个是不正确的,因为它们没有提供处理函数来 bind。声明:

$(window).bind('scroll');

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

...需要是这样的:

$(window).bind('scroll',function (){
    checkScroll();
});
// OR you don't actually need the anonymous function,
//    you can bind checkScroll directly:
$(window).bind('scroll',checkScroll);

// AND
function scroll(){ $(window).bind('scroll',checkScroll);}

(另请注意,在第 4 行,您$从 开始就缺少(window).bind('scroll'),但我认为这只是问题中的一个错字,而不是您的真实代码。)

于 2012-07-11T06:28:42.837 回答