diff options
author | David Gay <david@davidgay.org> | 2021-06-09 21:49:46 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-06-09 21:49:51 -0400 |
commit | 779f19c5b35f7c8c7690dac9fbf4606f49cfffde (patch) | |
tree | 115131c535b3b2577e2f92eb1e914d79a17feb6b /app/javascript/channels | |
parent | 40db33e9789f85b55bdf3dc02fe6c6a7547a2378 (diff) |
Only scroll chat and results output to the bottom on new content when already scrolled to the bottom or near the bottom
Diffstat (limited to 'app/javascript/channels')
-rw-r--r-- | app/javascript/channels/chat_room_channel.js | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/app/javascript/channels/chat_room_channel.js b/app/javascript/channels/chat_room_channel.js index 8dfa47f..af29953 100644 --- a/app/javascript/channels/chat_room_channel.js +++ b/app/javascript/channels/chat_room_channel.js @@ -15,8 +15,12 @@ consumer.subscriptions.create("ChatRoomChannel", { node.innerHTML = data.html; var chatOutputElement = document.getElementById("chat_output"); chatOutputElement.appendChild(node); - chatOutputElement.scrollTo({ - top: chatOutputElement.scrollHeight, left: 0, behavior: 'smooth' - }); + + // If scrolled to the bottom or near the bottom, then smooth scroll to the bottom. + if ((chatOutputElement.scrollTop + 100) >= (chatOutputElement.scrollHeight - chatOutputElement.offsetHeight)) { + chatOutputElement.scrollTo({ + top: chatOutputElement.scrollHeight, left: 0, behavior: 'smooth' + }); + } } }); |