php自动滚动问题.js

时间:2012-09-18 09:57:29

标签: php javascript html chat

是否有任何SIMPLE方法可以在php / html聊天框中创建自动滚动? 我尝试了一些东西,但它们都干扰了页面上的其他.js元素,最终都无法正常工作。

查看聊天室的实时代码点击此处!

这是我现在拥有的东西

// jQuery Document
 $(document).ready(function(){
//If user submits the form
$("#submitmsg").click(function(){   
    var clientmsg = $("#usermsg").val();
    $.post("post.php", {text: clientmsg});              
    $("#usermsg").attr("value", "");
    return false;
});

//Load the file containing the chat log
function loadLog(){     
    var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
    $.ajax({
        url: "log.html",
        cache: false,
        success: function(html){        
            $("#chatbox").html(html); //Insert chat log into the #chatbox div               
            var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
            if(newscrollHeight > oldscrollHeight){
                $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
            }               
        },
    });
}
setInterval (loadLog, 2500);    //Reload file every 2.5 seconds

//If user wants to end session
$("#exit").click(function(){
    var exit = confirm("Are you sure you want to end the session?");
    if(exit==true){window.location = 'index.php?logout=true';}

});

});

1 个答案:

答案 0 :(得分:1)

我怀疑你获得滚动高度的重量不起作用。我试图在FF的命令行中输入它,但没有获得int或对象。

不要试图获得滚动高度。向下滚动......还有更多。

$("#chatbox").animate({ scrollTop: 99999 }, 'normal');

我只尝试使用FF,但应该适用于大多数浏览器。

然后,你的成功功能很简单:

success: function(html){        $("#chatbox").animate({ scrollTop: 99999 }, 'normal');

            $("#chatbox").html(html);
            $("#chatbox").animate({ scrollTop: 99999 }, 'normal');
}       

$( “msgln”)。最后()。偏移()。顶部

相关问题