Skip to content

Instantly share code, notes, and snippets.

@PaulAsaf2
Created December 2, 2024 17:45
Show Gist options
  • Save PaulAsaf2/ac5d21296b1d489ef50adad925a1efcd to your computer and use it in GitHub Desktop.
Save PaulAsaf2/ac5d21296b1d489ef50adad925a1efcd to your computer and use it in GitHub Desktop.
Авто скролл при нахождении на нижней границе чата
let autoScroll = true;
function checkAtBottom() {
const atBottom = Math.abs(chat.scrollHeight - chat.clientHeight - chat.scrollTop) <= 2;
autoScroll = atBottom ? true : false;
}
function moveToBottom(el, force = false) {
if (autoScroll || force) {
el.scrollIntoView({ behavior: 'smooth', block: 'end' });
}
}
// где-то где надо добавить скролл
await new Promise(resolve => setTimeout(resolve, 10));
moveToBottom(pEl, true);
chat.addEventListener('scroll', checkAtBottom);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment