Javascript 当用户手动更改滚动位置时,如何在ajax定时器异步回发期间停止保持滚动位置?
我使用多行文本框作为聊天窗口,使用异步回发。我可以保持滚动位置,但当用户阅读聊天信息时,文本框会自动滚动到底部。我能够保持用户所在的位置或使其自动向下滚动。然而,我想两者兼而有之 这是我的密码:Javascript 当用户手动更改滚动位置时,如何在ajax定时器异步回发期间停止保持滚动位置?,javascript,asp.net,ajax,c#-4.0,Javascript,Asp.net,Ajax,C# 4.0,我使用多行文本框作为聊天窗口,使用异步回发。我可以保持滚动位置,但当用户阅读聊天信息时,文本框会自动滚动到底部。我能够保持用户所在的位置或使其自动向下滚动。然而,我想两者兼而有之 这是我的密码: var prm = Sys.WebForms.PageRequestManager.prm.add_beginRequest(BeginRequestHandler); prm.add_endRequest(EndRequestHandler); function BeginRequestHandle
var prm = Sys.WebForms.PageRequestManager.prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
xPos = $get('txtChatBox').scrollLeft;
yPos = $get('txtChatBox').scrollTop;
zpos =$get('txtChatBox').scrollHeight;
}
function EndRequestHandler(sender, args) {
$get('txtChatBox').scrollLeft = xPos + 1;
$get('txtChatBox').scrollTop = yPos + 1;
$get('txtChatBox').scrollTop =zpos+1;
}
有人能帮我吗
提前感谢您可以使用检测用户是否已手动滚动
var xPos, yPos, hasScrolled, timer, box = $get('txtChatBox');
box.onscroll = function(e) {
hasScrolled = true;
/* if you want to invalidate the flag after the user has not scrolled
for a certain period, use
clearTimeout(timer);
timer = setTimeout(function() {
hasScrolled = false;
}, 60000) // 1 min for example
*/
};
function BeginRequestHandler(sender, args) {
xPos = box.scrollLeft;
yPos = box.scrollTop;
zPos = box.scrollHeight;
hasScrolled = false;
}
function EndRequestHandler(sender, args) {
if (!hasScrolled) {
box.scrollLeft = xPos + 1;
box.scrollTop = zPos+1;
}
}
或者,您只需将存储的scrollTop(yPos
)与当前的scrollTop进行比较,即可检测用户是否已滚动(而不是返回):
你应该包括你正在使用的代码,并明确哪些部分有问题。删除,使其成为问题的一部分,并格式化它。
function EndRequestHandler(sender, args) {
if (box.scrollTop == yPos) {
box.scrollLeft = xPos + 1;
yPos = box.scrollTop = zPos+1;
}
}