【问题标题】:php autoscroll issues with .js.js 的 php 自动滚动问题
【发布时间】:2012-09-18 09:57:28
【问题描述】:

是否有任何简单的方法可以在 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';}

});

});

【问题讨论】:

    标签: php javascript html chat


    【解决方案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").last().offset().top

    【讨论】:

    • PS:在意识到没有必要获得偏移量之前,我正在接受这样的偏移量:$(".msgln").last().offset().top跨度>
    • 但现在它不允许我向上滚动 o.O
    • 我看到你现在采取了另一条路:现在把 cmets 放在一个 div 中 :) 祝你的网站好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多