Jump to content

jquery $(window).bind('scroll')?


shortysbest

Recommended Posts

I've got everything working well except for one thing. My website is primarily hash based, so when i am using the $(window).unbind('scroll') it doesn't work for other pages that use this code to load more comments, so I need to rebind the scroll event to window when hash changes, like:

 

$(window).hashchange(function(){	

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

});

 

However that doesn't work. The code I ended up using for loading comments is:

 

var finished = true;
///////////////////
$(function () {
$(window).scroll(function () {
if ($(window).height() + $(window).scrollTop() >= $(document).height()-135) {
loading();
if(finished)
{
finished = null;
if(get('id'))
{
var id = get('id');	
}
else
{
var id = '';	
}
var f_post_id = $(".stream li:last").attr('id');

if(stream_loaded)
{

$.ajax({
        type: "POST",
        url: "ajax/load_old_posts.php",
        data: "id="+id+"&post_id="+f_post_id,
        cache: false,
        success: function(data) {

	var check_data = data.replace(/^\s+|\s+$/g, '');
	if(check_data)
	  {
          $(".stream li:last").after(data);
	  $(".stream li").fadeIn("slow");
	  }
	 var lpostid = $(".stream li:last").attr('id');
	if(last_id==lpostid)
	{
		$(window).unbind('scroll');
	}
	finished = true;
	end_loading();
	}

      });  

}
}}
});
});

Link to comment
Share on other sites

I must say that I have no idea what "hash based" website means.

But you shouldn't need to re-bind the other events, just don't wipe them out in the first place.

function loadComments(){
...
}
// then use:
$(window).bind('scroll', loadComments);
// and:
$(window).unbind('scroll', loadComments);
//as you need

At least I think this speaks to the issue your having...

Link to comment
Share on other sites

Thank you, I got it working how I wanted.

And as to what I called a hash based website I mean it uses javascript to load all of the content rather then just regular pages.

for instance my website that's hash based the url looks like www.kithell.com#/home  rather than something like  www.kithell.com/home  or www.kithell.com?page=home

This way it doesn't have to reload all of the head content, such as the Jquery files, css, other javascript files that are large. Also allows for a lot of nice features other websites cannot have with a regular navigation system. For instance mine has it so you can watch youtube videos and switch through the pages while continuing to watch it without ending it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.