我正在开发与彗星的聊天,我希望 Slimscroll 仅在 div 更新时向下滚动,因为我的聊天脚本通过向 div 添加新消息来更新 div。到目前为止,我能够让 Slimscroll 每 2 秒向下滚动一次。
问题
但问题是它会向下滚动 div 是否已更改,因此,用户无法滚动到顶部。messages_content
仅当具有 id 的 div发生更改或大小增加时,如何使其向下滚动?
这是我的 slimscroll 代码
$(".slimscroll").slimscroll({
height: "300px",
color : "#0073b7",
alwaysVisible: true,
size: "10px",
start: 'bottom',
railVisible: true,
railColor: '#0073b7',
railBorderRadius: 0
}).css("width", "100%");
// update content every second
setInterval(function(){
// update slimscroll every time content changes
var en_bas = $('.slimscroll').slimscroll({ scrollBy: '400px' });
}, 2000);
这是我的 Comet 代码,它使用prototypejs 从数据库中获取信息。
<script type="text/javascript">
var Comet = Class.create();
Comet.prototype = {
timestamp: 0,
url: './messages_private_travail.php?id_du_membre_envoye=<?php echo $id_ojm_peoples; ?>&id_de_l_autre_originale=<?php echo $id_de_l_autre_originale; ?>&id_message=<?php echo addslashes(@$_GET['id_message']); ?>&chat_between=<?php $chat_between = hash('sha512',"Chat Between $id_ojm_peoples AND $id_de_l_autre_originale"); echo $chat_between; ?>',
noerror: true,
initialize: function() { },
connect: function()
{
this.ajax = new Ajax.Request(this.url, {
method: 'get',
parameters: { 'timestamp' : this.timestamp },
onSuccess: function(transport) {
// handle the server response
var response = transport.responseText.evalJSON();
this.comet.timestamp = response['timestamp'];
this.comet.handleResponse(response);
this.comet.noerror = true;
},
onComplete: function(transport) {
// send a new ajax request when this request is finished
if (!this.comet.noerror)
// if a connection problem occurs, try to reconnect each 5 seconds
setTimeout(function(){ comet.connect() }, 5000);
else
this.comet.connect();
this.comet.noerror = false;
}
});
this.ajax.comet = this;
},
disconnect: function()
{
},
handleResponse: function(response)
{
//$('messages_content').innerHTML += '<div>' + response['msg'] + '</div>';
$('messages_content').innerHTML += ' ' + response['msg'] + ' ';
//On appel la fonction qui permet de jouer le son
jouer_son('soundmanager2/audio/beep.mp3');
//On applique la fonction qui montre que le message a ete lu par le membre
mettreajour('nombre_nouveaux_messages', 'messages_lire_marquer_comme_lu.php?id_du_membre_envoye=<?php echo $id_ojm_peoples; ?>&id_de_l_autre_originale=<?php echo $id_de_l_autre_originale; ?>&id_message=<?php echo addslashes(@$_GET['id_message']); ?>&chat_between=<?php $chat_between = hash('sha512',"Chat Between $id_ojm_peoples AND $id_de_l_autre_originale"); echo $chat_between; ?>', '&id_ojm_peoples=<?php echo $id_ojm_peoples; ?>');
},
doRequest: function(request)
{
new Ajax.Request(this.url, {
method: 'get',
parameters: { 'msg' : request, evalScripts: true }
});
}
}
var comet = new Comet();
comet.connect();
</script>