Created
December 2, 2024 17:45
-
-
Save PaulAsaf2/ac5d21296b1d489ef50adad925a1efcd to your computer and use it in GitHub Desktop.
Авто скролл при нахождении на нижней границе чата
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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