绑定/取消绑定滚动

时间:2012-07-11 06:13:58

标签: jquery ruby-on-rails ajax scroll

我在滚动上调用一个函数,当我的目的被提供时,我解除了滚动的绑定。但是在单击菜单选项卡上我想再次绑定滚动。我们重新绑定滚动后解除它与刷新页面。这是我的代码。

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

1 个答案:

答案 0 :(得分:0)

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

$(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'),但我认为这只是问题的一个错字,而不是真实的代码。)