jQuery
我正在尝试使用/设置一个简单的聊天系统Ajax
,但是滚动部分的行为很奇怪。当我发布某些内容或收到某些内容时,滚动条会按照请求向上而不是向下。这是代码:
$('form[name="iview-messages-chat"]').submit(function(e) {
e.preventDefault();
var data = {};
data.msg = $('textarea', this).val();
data.time = $('#iview-messages-pane .iview-messages-item:last').attr('data-time');
data.uid = $('input[name="uid"]', this).val();
$.ajax({
type: 'POST',
url: 'index.php?page=messages&action=send',
data: data,
dataType: 'json',
}).done(function(data) {
$.each(data, function(index,value) {
var nm = '<div class="clearfix iview-messages-item" style="height:auto;min-height:60px;width:730px;margin:0;" data-cid="'+value.id+'" data-time="'+value.time+'">';
nm += '<img style="margin-top: 5px;" src="'+value.picture+'" />';
nm += '<div class="iview-messages-item-info" style="width:630px;">';
nm += '<div class="iview-messages-item-info-row" style="width:inherit;">';
nm += '<a href="'+value.profile+'">'+value.first_name+' '+value.last_name+'</a>';
nm += '<span style="float:right;font-size:11px;">'+value.elapsed_time+'</span>';
nm += '</div>';
nm += '<div class="iview-messages-item-info-row">';
nm += '<span>'+value.content+'</span>';
nm += '</div>';
nm += '</div>';
nm += '</div>';
$('#iview-messages-pane').append(nm);
});
$('form[name="iview-messages-chat"] textarea').val('');
if (!$.isEmptyObject(data))
{
$('#iview-messages-pane').animate({scrollTop: $('#iview-messages-pane').height()}, 800);
}
}).fail(function() {
console.log('error');
});
return false;
});
标记
div id="iview-messages-pane">
<div id="iview-messages-pane-handler" class="clearfix">
<div class="clearfix iview-messages-item" style="height:auto;min-height:60px;width:730px;margin:0;" data-cid="36" data-time="1340591805">
<img style="margin-top: 5px;" src="http://myiview.me/upload/1/4fda8a23cb3c3_200_200.jpg" />
<div class="iview-messages-item-info" style="width:630px;">
<div class="iview-messages-item-info-row" style="width:inherit;">
<a href="http://myiview.me/index.php?page=profile&uid=1">Filipe Matias</a>
<span style="float:right;font-size:11px;">19 hours ago</span>
</div>
<div class="iview-messages-item-info-row">
<span>Hmmmmmm yeeelloowww!</span>
</div>
</div>
</div>
</div>
</div>
有小费吗?