| 17 | const { username, room } = Qs.parse(location.search, { ignoreQueryPrefix: true }) |
| 18 | |
| 19 | const autoscroll = () => { |
| 20 | // New message element |
| 21 | const $newMessage = $messages.lastElementChild |
| 22 | // Height of the new message |
| 23 | const newMessageStyles = getComputedStyle($newMessage) |
| 24 | const newMessageMargin = parseInt(newMessageStyles.marginBottom) |
| 25 | const newMessageHeight = $newMessage.offsetHeight + newMessageMargin |
| 26 | // Visible height |
| 27 | const visibleHeight = $messages.offsetHeight |
| 28 | // Height of messages container |
| 29 | const containerHeight = $messages.scrollHeight |
| 30 | // How far have I scrolled? |
| 31 | const scrollOffset = $messages.scrollTop + visibleHeight |
| 32 | if (containerHeight - newMessageHeight <= scrollOffset) { |
| 33 | $messages.scrollTop = $messages.scrollHeight |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | //getting data from server in text form |
| 38 | socket.on('message', (message) => { |