Javascript DIV如何在聊天脚本中滚动?
我编写了一个聊天脚本,所有的问题都是滚动DIV层。我下载了一些聊天脚本,并在仔细观察后发现了以下内容 当添加新的聊天行时,添加聊天行后滚动条不会向下滚动。它的添加到DIV层的底部,因为滚动条在创建时不会造成任何干扰 我所做的: 在此之前,我使用javascript为每个固定的时间间隔向下滚动。执行此操作时,我无法手动向上滚动查看过去的行(由于间隔刷新滚动条移动到设置位置)。后来,我编写了一个javascript,可以在客户端单击时向下滚动Javascript DIV如何在聊天脚本中滚动?,javascript,ajax,chat,Javascript,Ajax,Chat,我编写了一个聊天脚本,所有的问题都是滚动DIV层。我下载了一些聊天脚本,并在仔细观察后发现了以下内容 当添加新的聊天行时,添加聊天行后滚动条不会向下滚动。它的添加到DIV层的底部,因为滚动条在创建时不会造成任何干扰 我所做的: 在此之前,我使用javascript为每个固定的时间间隔向下滚动。执行此操作时,我无法手动向上滚动查看过去的行(由于间隔刷新滚动条移动到设置位置)。后来,我编写了一个javascript,可以在客户端单击时向下滚动OnClientClick,但这样做时,我只能向下滚动Ch
OnClientClick
,但这样做时,我只能向下滚动Chat sender-side
,但当添加新的聊天线路时,它无法在Chat receiver-side
向下滚动
我下载了很多聊天脚本,并检查了如何处理这个问题,但我找不到任何解决方案。我猜这是jQuery的工作(不确定),有人能告诉我如何解决这个问题吗
我很抱歉,如果你没有理解我的问题,因为我无法解释它比上述更详细。不过,我可以根据您的要求提供更多信息
我使用的语言是ASP.NET、AJAX更新面板、使用新值更新div的计时器刻度,到目前为止,Java脚本仅用于向下滚动元素。您的聊天“屏幕”应如下所示:
<div id="chat">
<div class="wrapper">
<!-- chat messages go here -->
</div>
</div>
然后需要绑定一个事件“addMessage”,其工作原理如下:
$('#chat').bind('addMessage', function(e, message) {
var $this = $(this),
// store whether it's at the bottom before appending the message
scroll = $this.hasClass('atBottom');
// then append the message
$this.find('.wrapper').append(message);
if (scroll) {
// measure the new maxScroll and scroll to it.
var wrap = $this.find('.wrapper'),
height = $this.height(),
maxScroll = wrap.height() - height
$this.scrollTop(maxScroll);
}
})
$('button').click(function() {
$('#chat').trigger('addMessage', 'asdgagasdg<br/>');
});
$('#chat').bind('addMessage',函数(e,message){
变量$this=$(this),
//在附加消息之前,请存储它是否位于底部
scroll=$this.hasClass('atBottom');
//然后附加消息
$this.find('.wrapper').append(消息);
如果(滚动){
//测量新的maxScroll并滚动至它。
var wrap=$this.find('.wrapper'),
高度=$this.height(),
maxScroll=wrap.height()-height
$this.scrollTop(maxScroll);
}
})
$(“按钮”)。单击(函数(){
$(“#chat”).trigger('addMessage','asdgagasdg
);
});
下面是一个例子:
检查可能的“谢谢”副本,伙计,这对我很有用。再次感谢。你消除了我几个月来的疑虑。但是伙计,有没有一种方法可以用javascripts实现同样的功能?你是说不用jQuery?是的!不使用jQuery!有可能吗?我能实现同样的吗?我在等待你的回答。!
$('#chat').bind('addMessage', function(e, message) {
var $this = $(this),
// store whether it's at the bottom before appending the message
scroll = $this.hasClass('atBottom');
// then append the message
$this.find('.wrapper').append(message);
if (scroll) {
// measure the new maxScroll and scroll to it.
var wrap = $this.find('.wrapper'),
height = $this.height(),
maxScroll = wrap.height() - height
$this.scrollTop(maxScroll);
}
})
$('button').click(function() {
$('#chat').trigger('addMessage', 'asdgagasdg<br/>');
});