Patrick Reck
Patrick Reck

Reputation: 11374

JQuery scroll to bottom

I am using this function to update a chat conversation. The function runs every 2 seconds. When i scroll up, the update causes the page to scroll down again automaticly, which prevents me from reading old messages. How do i prevent this?

function opdaterPost(til, fra){
    $('.beskeder_vis').load('calls/opdater_post.php?til=' + til + '&fra=' + fra);
    $("#beskeder_vis").animate({ scrollTop: $("#beskeder_vis").prop("scrollHeight") }, 1500);   
}

Upvotes: 0

Views: 211

Answers (1)

designosis
designosis

Reputation: 5263

There are several approaches, depending on which behavior you want...

  1. Disable the scroll animation entirely.
  2. Make the scroll conditional on position ... if ($("#beskeder_vis").offset = the bottom) then animate (so it only animates when scrolled to the bottom)
  3. Store contents of #beskeder_vis in a variable, do the load, and then if the new content is different from the variable, do the animate (so it only animates when new content appears)

Upvotes: 1

Related Questions