Jump to content

Conditional Scroller


bdichiara

Recommended Posts

I am having the hardest time trying to write a condition for scrolling in an AJAX chat app i'm writing. After the AJAX request that updates the log, i'm using

objDiv.scrollTop = objDiv.scrollHeight;

I was trying to write a condition so if the user decides to scroll up to read older text, that it will not scroll them to the bottom until they are finished and scroll back down themselves. When doing this, i ran into a couple of issues. So, here's what I'm looking to do:

1. when the chat is initialized, i want it to perform the scroll function (above). It's possible that there already be messages in the window, so start them at the bottom.

2. if they are within 20px (or 30) from the bottom, then scroll them on down the rest of the way

 

There may be a better way to do this, if anyone has any suggestions. Otherwise i guess a series of conditions will help it behave normally.

 

Thanks.

Link to comment
Share on other sites

Ok, i think i got this figured out:

var initialized = false;

... then inside the function ...

var objDiv = document.getElementById("message_window");
var sHeight = objDiv.scrollHeight;
var sTop = objDiv.scrollTop;
var mHeight = objDiv.clientHeight;
if(initialized){
if((sTop+mHeight) >= (sHeight-30) && (sHeight > (mHeight+25))){
	objDiv.scrollTop = objDiv.scrollHeight;
}
} else {
var theDiv = document.getElementById("message_window");
theDiv.scrollTop = theDiv.scrollHeight;
initialized = true;
}

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.