jScrollPane是一個非常棒的jQuery插件,它修改了默認的瀏覽器滾動條。我正在開發一個將使用jScrollPane的聊天應用程序。它迄今爲止效果很好,除了我無法確定最大滾動高度的事實。這是我想要做的:如何在jScrollPane中獲取最大滾動高度?
$(function()
{
$('#chatPane').jScrollPane();
var api = $('#chatPane').data('jsp');
setInterval(function()
{
$.ajax(
{
url: "Home/GetMessage",
type: "POST",
success: function (response)
{
// This next line is where the issue occurs. I need to check if the current scroll position is equal to the max scroll position. This is to implement the auto scroll if the user has scrolled to the bottom of the chat.
var atBottom = (api.getContentPane().getContentPositionY() == api.getContentPane().getMaxScrollHeight()??????);
api.getContentPane().append(response);
api.reinitialise();
if (atBottom)
api.scrollToBottom();
},
error: function (xhr, textStatus, errorThrown)
{
alert(textStatus);
}
});
}, 1000);
});
getMaxScrollHeight不存在。我需要一些方法來確定最大滾動高度。
編輯:我得到它與ajax成功函數的以下更改一起工作。但如果有人知道比滾動到底部更好的方式,獲得當前位置,然後滾動回到以前的位置,將不勝感激。
success: function (response)
{
var currentPosition = api.getContentPositionY();
api.scrollToBottom();
var maxPosition = api.getContentPositionY();
api.scrollToY(currentPosition);
var atBottom = (currentPosition == maxPosition);
api.getContentPane().append(response);
api.reinitialise();
if (atBottom)
api.scrollToBottom();
}
有趣的想法。我怎麼會模擬一個scrollToBottom()雖然,有什麼想法? – Chev 2010-12-03 19:35:46
我可以想到兩種方法:第一種方法是創建一個與頁面上的實際對象具有相同屬性的內存中Javascript副本,並在其上運行命令。第二種方法是存儲當前的滾動位置,針對活動對象運行命令,然後恢復先前的滾動位置。這可能會導致用戶界面跳轉,因此您必須對其進行測試以查看它是否可用。 – ChessWhiz 2010-12-03 19:39:18