Autoscroll won't stop scrolling .js/jquery/ajax issue
-
02-07-2021 - |
Question
My chat box won't stop autoscrolling (it won't let me scroll up), I know where the problem lies... however I don't know how to get around it. I need the chat box to autoscroll however I want to be able to scroll up at the same time.
Here's a live example of the problem.
//Load the file containing the chat log
function loadLog(){
$("#chatbox").animate({ scrollTop: 99999 }, 'normal');
$.ajax({
url: "log.html",
cache: false,
success: function(html){ $("#chatbox").animate({ scrollTop: 99999 }, 'normal');
$("#chatbox").html(html);
$("#chatbox").animate({ scrollTop: 99999 }, 'normal');
}
});
}
Solution
You can check when the scoll position is. if it as the bottom then scroll to bottom
//Load the file containing the chat log
function loadLog(){
// remove $("#chatbox").animate({ scrollTop: 99999 }, 'normal');
$.ajax({
url: "log.html",
cache: false,
success: function(html){
var chatbox= $("#chatbox");
var atBottom = (chatbox[0].scrollHeight - chatbox.scrollTop() == chatbox.outerHeight());
chatbox.html(html);
if (atBottom )
chatbox.animate({ scrollTop: 99999 }, 'normal');
}
});
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow