admin管理员组文章数量:1291029
I am going to added my message and it will be automatically added to textarea and it will scroll down .I have my own codes.But it seems does not work. I tried also this code, but hard to manupulate. Here
EDIT:
Send
<textarea name="msg" id="area" ></textarea><br/>
<a href="#" onclick="submitChat()"> Send </a>
<div id="chatlogs">
</div>
var onKeyDown = function(e) {
if (e.which == 13) {
e.preventDefault();
submitChat();
$("#area").val("");
}
};
$("#area").on("keydown", onKeyDown);
function submitChat(){
if( form1.msg.value ==''){ //tell the function to exit if one of the fields are blank
alert("All fiels are mandatory");
return;
}
form1.uname.readonly=true;
form1.uname.style.border='none';
$('#imageload').show();
var uname= form1.uname.value; //storing the value pf fields into js variable which will pass on insert.php myimage
var msg = form1.msg.value;
var myimage = form1.myimage.value;
var room = form1.room.value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status ==200)
{
document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
$('#imageload').hide();
}
}
xmlhttp.open('GET','insert.php?uname='+uname+'&msg='+msg+'&myimage='+myimage+'&room='+room,true);
xmlhttp.send();
}
$(document).ready(function(e)
{
$.ajaxSetup({cache:false});
setInterval(function() {$('#chatlogs').load('logs.php');}, 2000);
});
I am going to added my message and it will be automatically added to textarea and it will scroll down .I have my own codes.But it seems does not work. I tried also this code, but hard to manupulate. Here
EDIT:
Send
<textarea name="msg" id="area" ></textarea><br/>
<a href="#" onclick="submitChat()"> Send </a>
<div id="chatlogs">
</div>
var onKeyDown = function(e) {
if (e.which == 13) {
e.preventDefault();
submitChat();
$("#area").val("");
}
};
$("#area").on("keydown", onKeyDown);
function submitChat(){
if( form1.msg.value ==''){ //tell the function to exit if one of the fields are blank
alert("All fiels are mandatory");
return;
}
form1.uname.readonly=true;
form1.uname.style.border='none';
$('#imageload').show();
var uname= form1.uname.value; //storing the value pf fields into js variable which will pass on insert.php myimage
var msg = form1.msg.value;
var myimage = form1.myimage.value;
var room = form1.room.value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status ==200)
{
document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
$('#imageload').hide();
}
}
xmlhttp.open('GET','insert.php?uname='+uname+'&msg='+msg+'&myimage='+myimage+'&room='+room,true);
xmlhttp.send();
}
$(document).ready(function(e)
{
$.ajaxSetup({cache:false});
setInterval(function() {$('#chatlogs').load('logs.php');}, 2000);
});
Share
Improve this question
edited Apr 12, 2015 at 5:27
Ikaros
asked Apr 12, 2015 at 4:16
IkarosIkaros
1792 silver badges7 bronze badges
4 Answers
Reset to default 5You may use
document.getElementById('chatlogs').lastChild.scrollIntoView(false)
if you have messages in separate elements (you should).
In your ajax success callback, you can use auto scroll the div element:
var element = document.getElementById('chatlogs');
element.scrollTop = element.clientHeight;
//You can even use
// element.scrollTop = element.scrollHeight;
Based on div height, it will scroll to the bottom of the div.
setInterval(function() {
$('#chatlogs').load('logs.php');
var element = document.getElementById('chatlogs');
element.scrollTop = element.clientHeight;
}, 2000);
If I were you, I'd give my element an overflow-y: scroll
in my CSS and add the following to my success callback
var element = document.getElementById('chatlogs');
element.scrollTop = element.scrollHeight;
I'm using it in a React application and it works fairly well. Just make sure the message is added before you scroll the element.
This worked for me
//check condition before inserting messages into chatbox div
if(window.pageYOffset+740==html.scrollHeight)
{
flag=true;
}
/*
append messages into the chatbox
*/
//now if the user is in the last child it will scroll
if(flag)
{
html.scrollTop=messages.scrollHeight;
flag=false;
}
本文标签: javascriptAuto scroll down when new message is addedStack Overflow
版权声明:本文标题:javascript - Auto scroll down when new message is added - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741500977a2382054.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论