1

When i am using the above function in javascript div to auto scroll in chat box but i am not able to scroll up to bottom.

window.setInterval(function() {
 var elem = document.getElementById('chatlog');
elem.scrollTop = elem.scrollHeight;
}, 10);

The chatlog is a div in my code and I have put above function to scroll the replies. Now I cannot scroll up.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
Abdul
  • 13
  • 3

2 Answers2

2

This is logical mistake. In your code every 10 ms your script will scroll div to bottom. There are many variants how to implement autoscroll behaviour. One of them use flag, which will set up by checkBox (autoscroll). If checkBox is selected script will scroll down, if deselected script will do nothing.

Something like:

window.setInterval(function() {
 if (needAutoScroll) {
     var elem = document.getElementById('chatlog');
     elem.scrollTop = elem.scrollHeight;
 }
}, 10);
Sabik
  • 1,599
  • 1
  • 11
  • 10
0

My guess is , since it is scrolling down to the bottom of the div every second , you are not able to scroll up. So on scroll up you have to clear the setInterval function.

You can try this in you chatlog div.

<div id="chatlog" onscroll="myStopFunction()">

function myStopFunction() {

if document.body.scrollTop <= 0 {
 console.log("scrolling down")
} else {
console.log("scrolling up");
 clearInterval(myVar);
}

}

Again on scroll down you can trigger the setInterval function.

Sooraj
  • 9,717
  • 9
  • 64
  • 99