Javascript 如何在收到消息时自动向下滚动到列表

Javascript 如何在收到消息时自动向下滚动到列表,javascript,html,Javascript,Html,大家好,我有一个div,它列出了消息,下面是代码 <section class="portfolio-section masonry-style"> <div class="container"> <h2>TOPLU KONUŞMA</h2> <div class="container" style="max-height:500px;min-height:500px;overflow-y: scr

大家好,我有一个div,它列出了消息,下面是代码

  <section class="portfolio-section masonry-style">
    <div class="container">
        <h2>TOPLU KONUŞMA</h2>
        <div class="container" style="max-height:500px;min-height:500px;overflow-y: scroll;overflow-x: hidden;">
            <br />
            <input type="hidden" id="displayname" />
            <ul id="discussion"></ul>
        </div>
        </div>  

</section>
<div class="container">
    <div class="container">
        <div id="contact-form">
            <textarea name="comment" id="message" placeholder="Mesajınız"></textarea>
            <div class="submit-area">
                <input type="submit" id="sendmessage" value="Gönder">
                </div>
            </div>
    </div>
    </div>
剧本

    <script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.-->
    <script>
        $(function () {
            var currentMember = '@Html.Raw(@ViewBag.Name)';
            // Reference the auto-generated proxy for the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {
                // Add the message to the page.
                $('#discussion').append('<li><strong>' + htmlEncode(name)
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };

            // Get the user name and store it to prepend to messages.
            $('#displayname').val(currentMember);
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>

因此,正如您所看到的,当消息到来时,它不会自动向下滚动。这是实时聊天。因此,如何在收到消息时自动向下滚动呢?首先,请为下一行添加id。如果没有该div的ID,Jquery将无法选择正确的div,因为您有多个div.container

然后使用jquery滚动div,使用以下代码:

$'divmessageContainer'。scrollTop$'divmessageContainer'[0]。scrollHeight

输入密码

1在添加新邮件后立即添加

$'discussion'.append+htmlEncodename+':'+htmlEncodemessage+

2清除输入框后


$'message'.val.focus

在你提供一个演示之前还不清楚。上传了一张图片@BhojendraNepalcould你可以从浏览器和javascript中提供消息框的来源吗?@Woody分享了一切。我只需要在列表更新时自动滚动。我使用实时聊天尝试在javascript中的消息附加后添加以下行:$'uldiscussion'。scrollTop$'uldiscussion'[0]。scrollHeight;看看这是否有效