break off autoscroll if moved from bottom, tweak for button

This commit is contained in:
Kyle Drake 2024-03-26 15:59:09 -05:00
parent a975c9c2be
commit 4e05121dfc
3 changed files with 39 additions and 9 deletions

View file

@ -105,11 +105,33 @@ document.addEventListener('DOMContentLoaded', () => {
// Keeps the chat box scrolled to the bottom
// Function to scroll to the bottom
function scrollToBottom() {
chatBox.scrollTop = chatBox.scrollHeight;
// Check if auto-scrolling is enabled
if (shouldAutoScroll) {
chatBox.scrollTop = chatBox.scrollHeight;
}
}
window.onload = scrollToBottom;
// Flag to keep track of whether auto-scrolling should be performed
let shouldAutoScroll = true;
window.onload = function() {
scrollToBottom();
// Detect manual scrolling by the user
chatBox.addEventListener('scroll', () => {
// Calculate the distance from the bottom
const distanceFromBottom = chatBox.scrollHeight - chatBox.scrollTop - chatBox.clientHeight;
// If the distance from the bottom is small (or zero), the user is at the bottom
if (distanceFromBottom < 1) {
shouldAutoScroll = true;
} else {
// If the user has scrolled up, disable auto-scrolling
shouldAutoScroll = false;
}
});
};
const observer = new MutationObserver(scrollToBottom);
observer.observe(chatBox, { childList: true });