1

I have a script doing a task much like a regular chat. Whenever I load new messages I fill a div like this:

....
<div class="conversation">
   <span class="sender">Sender: </span><span>Message sent by sender..</span>
</div>
....

Then, using jQuery I do something like this:

var lastOne = $("div.conversation:last", itemBox).offset();
itemBox.scrollTop(lastOne.top);

When I'm having a few (~40 messages) it works ok, but when the list grows too large, it starts calculating the offset wrong. This happens specially with large messages, taking over 3 lines. I'm not using any float inside the chat box (parent of all div.conversation), so I'm really shocked here...

Thanks in advance

2
  • Can I ask what the 2nd argument in your code line is? What's itemBox in $("div.conversation:last", itemBox).offset();? I didn't know that you could pass 2 arguments to jquery when selecting.. Commented Jun 27, 2011 at 18:38
  • The second argument is the context of the function. You can pass to jQuery that second argument and it will look for your search only inside the supplied context, if no context is supplied, the whole window is the context... Commented Jun 27, 2011 at 18:41

3 Answers 3

3

Try use .position() instead of .offset() if you can make it work for your needs.

http://api.jquery.com/position/

I've had a similiar problem with wrong offset calculations with jQuery

Sign up to request clarification or add additional context in comments.

1 Comment

Problems has improved a bit, works better on IE, but stills looses track of the conversation on FF (testing with FF4)
0

May be you can try using ViewPort ?

Comments

0

Okay,

I'm not much of a fan of doing this whole "answering my question" thing, but I'm less of a fan of leaving an open question. The position function does work on jQuery with some major browsers, but I haven't figured out why.. it dies with FFx 4, at the point I had to calculate the height of all the divs in the box, much like this:

var itemBox = $("#conversation5");
var loader = $("div.conversation", itemBox);
var sum = 0;

for (var i = 0; i < loader.length; i++)
   sum += $(loader[i]).innerHeight();

itemBox.scrollTop(sum);

It works flawlessly on all browsers, admitedly, not the prettiest solutions, but works nontheless.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.