【问题标题】:How to stop maintaining scroll position during ajax timer asynchronous postback when user manually changed the scroll position?当用户手动更改滚动位置时,如何在 ajax 计时器异步回发期间停止保持滚动位置?
【发布时间】:2023-03-29 15:51:01
【问题描述】:

我通过异步回发将多行文本框用作聊天窗口。我能够保持滚动位置,但是当用户阅读聊天消息时,文本框会自动滚动到最底部。我能够保持用户所在的位置或使其自动向下滚动。但是,我想两者都做。

这是我的代码:

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; 
}

谁能帮帮我?

提前致谢

【问题讨论】:

  • 您应该包含您正在使用的代码,并具体说明哪个部分有问题。
  • 删除,使其成为问题的一部分并格式化。

标签: javascript asp.net ajax c#-4.0


【解决方案1】:

您可以使用scroll event 来检测用户是否手动滚动离开。

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) 与当前的进行比较,以检测用户是否滚动离开(而不是返回):

function EndRequestHandler(sender, args) {
  if (box.scrollTop == yPos) {
    box.scrollLeft = xPos + 1; 
    yPos = box.scrollTop = zPos+1;
  }
}

【讨论】:

    猜你喜欢
    • 2011-02-28
    • 2011-09-28
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2011-07-31
    • 2019-08-03
    相关资源
    最近更新 更多