简体   繁体   English

具有垂直和水平滚动的HTML表

[英]HTML table with vertical and horizontal scrolling

There is a table in which the number of rows and columns to be displayed is not known in advance.That is generated in a JSP like 有一个表,其中不知道要显示的行数和列数,这是在类似JSP的JSP中生成的

  <thead>
   <tr>
     <c:foreach col=${Dcol} >
      ...
     </c:foreach>
   </tr>
   </thead>

ie width of the columns isn't fixed 即列的宽度不固定

When scrolling horizontally the header row should scroll horizontally. 水平滚动时,标题行应水平滚动。

When scrolling vertically the header row should remain fixed. 垂直滚动时,标题行应保持固定。

Although there is a solution here Scrollable table both horizontally and vertically with fixed header 尽管这里有一个解决方案,但具有固定标题的水平和垂直滚动表

I would like to know if i can use two separate tables (Table1 for header row and Table2 for all other rows) and synchronize their horizontal scrolling by binding a horizontal scroll event on second table to call a function which scrolls first table horizontally. 我想知道是否可以使用两个单独的表(表行用于标题行,表2用于所有其他行),并通过在第二个表上绑定一个水平滚动事件来调用一个水平滚动第一个表的函数来同步其水平滚动。 Can this be done through simple javascript as a table has only an onscroll event and no event for specific direction of scroll? 可以通过简单的javascript完成此操作,因为一个表只有一个onscroll事件,而没有针对特定滚动方向的事件?

Please provide any plain javascript solution or sequence of steps.Thanks 请提供任何简单的javascript解决方案或步骤序列。谢谢

You could use position:absolute on a window.onscroll event and determine for window.pageYOffset or document.body.scrollTop , like: 您可以在window.onscroll事件上使用position:absolute ,并确定window.pageYOffsetdocument.body.scrollTop ,例如:

//first the CSS
#headerContainer{
  padding:0; position:relative; overflow:visible;
}
#header{
  margin:0; padding:0; position:absolute; top:0; left:0; z-index:99;
}
//now the JavaScript
var win = window, doc = document, bod = doc.getElementsByTagName('body')[0];
function E(e){
  return doc.getElementById(e);
}
var pT = 0;
function nice(elementId, offsetParentTop){
  var hs = E(elementId).style;
  var sy = win.pageYOffset || bod.scrollTop;
  if(sy !== pT){
    pT = sy+offsetParentTop;
    hs.top = pT+'px';
  }
}
onscroll = function(){
  nice('header', 30);
}

You could use a series of Element.offsetTop attributes to determine your offsetParentTop argument. 您可以使用一系列Element.offsetTop属性来确定您的offsetParentTop参数。 It really depends how deep you have your header. 这实际上取决于标题的深度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM