0

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

这是我的代码:

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

谁能帮帮我吗?

提前致谢

4

1 回答 1

0

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

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;
  }
}
于 2013-04-04T13:46:15.663 回答