除非用户滚动,否则将滚动条保持在底部

时间:2017-11-15 15:05:31

标签: javascript jquery html css

PLZ帮助我编写脚本。

<script>
$ = jQuery;
var currentID = null;
var chatTimer = null;
var scrolled;

function fetch_data() {
  $.ajax({
    url: "select.php",
    method: "POST",
    success: function(data) {
      $('#live_data').html(data);
     }
  });
}

function fetch_chat() {

  $.ajax({
    url: "fetch_chat.php",
    method: "POST",
    data: {
      id: currentID
    },
    dataType: "text",
    success: function(data) {
      $("#chatbox").show();
      $('#messages').html(data);
      $("div.area").show();
      if(!scrolled){
      $('#messages').scrollTop($('#messages')[0].scrollHeight);
      scrolled=true;
      }
    }

  });

}

$(document).ready(function() {

 fetch_data();

  $(document).on('click', '.first_name', function() {
      scrolled=false;
    currentID = $(this).data("id1");
    setInterval(function() {
    fetch_chat();}, 500);
});



  $("#sub").click(function() {

    var text = $("#text").val();
    $.post('insert_chat.php', {
      id: currentID,
      msg: text
    }, function(data) {
      $("#messages").append(data);
      $("#text").val('');
      scrolled=false;
    });
  });

});
</script>

当用户&#39; a&#39;向用户发送消息&#39; b&#39;滚动条在一侧得到更新并移到底部但是在b方的情况下这不适用,并且类似地当用户&#39; b&#39;向用户发送消息&#39; a&#39;滚动条仅在b侧更新并移至底部。

0 个答案:

没有答案
相关问题