Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

javascript - Automatically Scroll Message List

I am making a message app with express and socket.io. The messages are appended to a list as shown below:

<div id="messageContainer">
    <ul id="messages"></ul>
</div>
socket.on('message', (content) => {
    $('#messages').append(`<li>${content.bold} ${content.std}</li>`);
    $('#messages').scrollTop = $('#messages').scrollHeight;
});

The messages are appended correctly appended however the scrolling does not occur. I get no error message and I am unsure why.

The full source code can be found on GitHub here.

Edit

I have added a console.log('scroll') to the bottom of the js call however nothing is shown.

socket.on('message', (content) => {
    $('#messages').append(`<li>${content.bold} ${content.std}</li>`);
    $('#messages').scrollTop = $('#messages').height();
    console.log('scroll')
});

This makes me think that scrolling isn't being called even thought the messages are being appended


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

jQuery objects have no property scrollHeight, change that to a function call of height.

$('#messages').scrollTop = $('#messages').height();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...