在结束请求上设置多个滚动条位置



我有一个经常回发的 ASP.NET 页面,所以我四处寻找解决方案来维护页面上可滚动div 的滚动条位置。我找到了一个适用于单个div的解决方案(jquery找到的第一个解决方案$get),但我正在尝试重新设计它以遍历每个div。

 var xPos = [];
 var yPos = [];
 var prm = Sys.WebForms.PageRequestManager.getInstance();
 prm.add_beginRequest(BeginRequestHandler);
 prm.add_endRequest(EndRequestHandler);
 function BeginRequestHandler(sender, args) {
     yPos = [];
     xPos = [];
     $('tableContainer').each(function (theIndex, element) {
         xPos[theIndex] = $(this).scrollLeft;
         yPos[theIndex] = $(this).scrollTop;
     });
 }
 function EndRequestHandler(sender, args) {
     $('tableContainer').each(function (theIndex, element) {
         $(this).scrollLeft = xPos[theIndex];
         $(this).scrollTop = yPos[theIndex];
     });
 }

这就是我到目前为止所得到的地方,但这似乎没有解决问题,我不确定它在哪里挂断。我把它放在jsfiddle中,以确保没有我丢失的语法错误,但我不太确定从这里开始。

有人可以提供一点见解吗?它似乎运行了一段时间,然后最终抛出索引越界错误;我认为在请求处理程序的开头重置数组至少可以防止这种情况。

经过一番修补,这就是我最终解决问题的方式。我意识到我使用的元素都具有相同的 ID,所以我遍历了整个类。

 var xPos = [4];
 var yPos = [4];
 var prm = Sys.WebForms.PageRequestManager.getInstance();
 prm.add_beginRequest(BeginRequestHandler);
 prm.add_endRequest(EndRequestHandler);
 function BeginRequestHandler(sender, args) {
     $(".tableContainer").each(function (theIndex, element) {
         xPos[theIndex] = $(this).scrollLeft;
         yPos[theIndex] = $(this).scrollTop;
     });
 }
 function EndRequestHandler(sender, args) {
     $(".tableContainer").each(function (theIndex, element) {
         $(this).scrollLeft = xPos[theIndex];
         $(this).scrollTop = yPos[theIndex];
      });

 }

最新更新