简体   繁体   English

ajax加载后的jQuery DIV AutoScroll

[英]jQuery DIV AutoScroll after ajax load

I have a simple chat application on a web page, i have an issue when trying to autoscroll the div only when the scroll bar is at the bottom. 我在网页上有一个简单的聊天应用程序,仅在滚动条位于底部时才尝试自动滚动div时出现问题。

I've tried this: 我已经试过了:

$("#line").load("x.php");
   var d = $('#line');
   d.scrollTop(d.prop("scrollHeight"));
   var refreshId = setInterval(function(){
      var isEnd = $('#line')[0].offsetHeight + $('#line')[0].scrollTop == $('#line')[0].scrollHeight;
      $('#line').append('<p class="triangle-isosceles right"><img src="images/user-img.jpg" style="height: 30px;padding-right: 5px;"/><b>Douglas:</b> prueba<br><small>Fecha</small></p>');
      console.log(isEnd);
      if(isEnd){
       var scrolle = $("#line").prop("scrollHeight") - $('#line').height();
       $("div#line").scrollTop(scrolle)  ;
      }
    }, 1000);

So when doing this using the append function works great, the issue comes when instead of append i refresh the content of the div using load() 因此,当使用append函数很好地执行此操作时,就会出现问题,而不是使用load()刷新div的内容而不是添加

$("#line").load("x.php");
   var d = $('#line');
   d.scrollTop(d.prop("scrollHeight"));
   var refreshId = setInterval(function(){
      var isEnd = $('#line')[0].offsetHeight + $('#line')[0].scrollTop == $('#line')[0].scrollHeight;
      $("#line").load('ajax/x.php');
      console.log(isEnd);
      if(isEnd){
       var scrolle = $("#line").prop("scrollHeight") - $('#line').height();
       $("div#line").scrollTop(scrolle)  ;
      }
    }, 1000);

This seems to break the autoscroll because the div is not autoscrolling on new messages. 这似乎破坏了自动滚动,因为div不会自动滚动新消息。

Appreciate your help 感谢您的帮助

Finally i achieved my goal by doing this: 最终,我做到了这一点:

var scrolled = false;
    $(window).load(function() {
       $("#line").load("x.php");
       var d = $('#line');
       d.scrollTop(d.prop("scrollHeight"));
       $("#line").on('scroll', function(){
            scrolled=true;
        });
       var refreshId = setInterval(function(){     
            $("#line").load('x.php');
          if($('#line')[0].offsetHeight + $('#line')[0].scrollTop == $('#linea')[0].scrollHeight){
              scrolled=false;
          }

          updateScroll();
        }, 1000);
    });

    function updateScroll(){
        if(!scrolled){
            $("#linea").scrollTop($("#linea").prop("scrollHeight"));
        }
    }

I define a var named scrolled and set it to false (the user didn't scroll the div), after that i load the conversations on the div #line, then automatically scroll to bottom of that div to show the last messages, add a listener to the scroll event, in case the user scroll the div then i set scrolled to true so the div do not scroll automatically. 我定义了一个名为scroll的var,并将其设置为false(用户没有滚动div),之后,我在div #line上加载了对话,然后自动滚动到该div的底部以显示最后一条消息,并添加一个滚动事件的侦听器,如果用户滚动div,则我将scroll设置为true,因此div不会自动滚动。

Do the refresh of the div element every second and check if the current scroll position is equal to the height of the div, if so then set the scrolled to false. 每秒刷新div元素,并检查当前滚动位置是否等于div的高度,如果是,则将滚动设置为false。

After that i run a function called updateScroll to scroll to the bottom if the scrolled var is false. 之后,如果滚动的var为false,我将运行一个名为updateScroll的函数以滚动到底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM