On this page, I have a chat bot that responds to a user's text input. I'm using the below code to append the chat bot's response onto the page as a <p>
tag.
This code is also supposed to scroll the page to the bottom of the window as new <p>
tags are appended, but it is not working correctly. After getting a few responses from the bot, the input box goes below the fold and there is no auto scroll; however, if I then scroll to the bottom and start typing to the bot again, it begins to auto scroll.
I adapted this code from another answer here. Any ideas what the issue is?
function jumpToPageBottom() {
$('html, body').scrollTop( $(document).height()-$(window).height());
console.log( "Document:" + $(document).height() );
console.log( "Window:" + $(window).height() );
}
function isAtBottom() {
console.log($(window).scrollTop() + " and " + $(window).height() + " equals " + $(document).height() + "?")
return $(window).scrollTop() + $(window).height() === $(document).height();
}
function respond(val) {
var wasAtBottom = isAtBottom();
console.log("Was at Bottom: " + wasAtBottom);
if (val == "") {
val = messageSorry;
}
$(".chatter").append("<p class='chatresponse'>" + val + "</p>");
console.log("Response Added");
if (wasAtBottom) {
jumpToPageBottom();
}
}