0

I am calling a function on scroll, when my purpose is served I am unbinding the scroll.But on the click of menu tab I want to bind the scroll again.Cant we re-bind the scroll after unbinding it with out refreshing the page.Here is my code.

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();
 });
Deepti Thakur
  • 38
  • 1
  • 4

1 Answers1

0

You can rebind a handler for the scroll event at any time, but two out of your three calls to .bind('scroll') are not correct because they don't supply a handler function to bind. The statements:

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

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

...need to be like this:

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

(Note also that on line 4 you're missing the $ from the start of (window).bind('scroll'), but I assume that's just a typo in the question not in your real code.)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • I can not give return false on a click event of gallery.If I do so , it doe not work properly. I tried giving return false inside scroll() function but still my problem persists. – Deepti Thakur Jul 11 '12 at 06:29
  • I didn't say anything about returning false, that was the other answer that has now been deleted. – nnnnnn Jul 11 '12 at 06:32
  • Hello, Thanks a lot for great help,with modifications suggested by u and by removing call to checkScroll() at the bottom in my code, Its working fine. – Deepti Thakur Jul 11 '12 at 06:40