JQuery desplazarse hacia abajo

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

preguntado el 31 de julio de 12 a las 15:07

1 Respuestas

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)

Respondido 31 Jul 12, 15:07

How do i find out what "the bottom" is? And how would i go on with your third suggestion? :-) - patricio rey

which behavior would you prefer? animate only when scrolled at bottom, or check if content has changed, or both? :D - neokio

If content changed sounds like the cooler option :-D However i'd like to hear how the animation only when on bottom suggestion would work out as well if you don't mind :-D - patricio rey

if i find time tomorrow, i'll make #3 into a jsfiddle for ya... but #2 is pretty self-explanatory i hope: If the div is scrolled all the way to the bottom, load new content ... otherwise, don't. - neokio

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.