自动滚动到页面底部的jQuery问题

时间:2017-05-17 02:17:35

标签: javascript jquery scroll scrolltop

this page上,我有一个响应用户文本输入的聊天机器人。我正在使用以下代码将聊天机器人的响应作为<p>标记附加到页面上。

此代码也应该在添加新的<p>标记时将页面滚动到窗口底部,但它无法正常工作。从机器人获得一些响应之后,输入框在折叠之下,并且没有自动滚动;但是,如果我滚动到底部并再次开始键入机器人,则会开始自动滚动。

我从另一个答案here改编了这段代码。任何想法是什么问题?

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();
  }
} 

0 个答案:

没有答案
相关问题