固定宽度为100%的DIV覆盖滚动条

7

我有一个页面,其中包含一个包含大量数据的表格的div。该div具有overflow-x:hidden;overflow-y:scroll;的溢出值。你只需要使用此div的滚动条来滚动,而不是页面滚动。但是,当你滚动时,表头(thead)是固定的,并且停留在div的顶部,但它遮住了滚动条。
我希望表格表头能够占据整个带有滚动条的div, 但它不应该覆盖滚动条。
我已在Chrome、Firefox和IE9中进行了测试,它们看起来都一样!

HTML

 <div style='position:absolute;bottom:0;height:396px;width:100%;overflow-y:scroll;overflow-x:hidden;'>
  <table id='select-customer-results' style='margin-top:-1px;position:fixed;width:100%;'>
   <tr>
    <th style='width:15%;'>Code</th>
    <th style='width:85%;'>Name</th>
   </tr>
   </table>
   <div style='height:37px;'></div>
   <table id='select-customer-results'>
    <!--A lot of rows!-->
   </table>
  </div>

CSS

table#select-customer-results{
 width:100%;
 margin-top:-5px;
}
table#select-customer-results tr{
 border-bottom:1px solid #797979;
}
table#select-customer-results tr:nth-child(even){
 background:#EBF2F7;
}
table#select-customer-results td,table#select-customer-results th{
 font-family:Helvetica, Arial, sans-serif;
 padding:5px;
 border:1px solid #797979; 
}
table#select-customer-results td{
 cursor:pointer; 
}
table#select-customer-results tr.clickable:hover{
 background-color:#BCC2C6;
}
table#select-customer-results th{
 color:#FFF;
 padding:7px;
 padding-top:10px;
 font-weight:bold;
 text-align:left;
 box-shadow:0 2px 3px 0 #474747;
 -webkit-box-shadow:0 2px 3px 0 #474747;
 -moz-box-shadow:0 2px 3px 0 #474747;
 background: #878787;
 background: -moz-linear-gradient(top,  #878787 0%, #6E6E6E 50%, #5C5C5C 51%, #7B7B7B 100%);
 background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#878787), color-stop(50%,#6E6E6E), color-stop(51%,#5C5C5C), color-stop(100%,#7B7B7B));
 background: -webkit-linear-gradient(top,  #878787 0%,#6E6E6E 50%,#5C5C5C 51%,#7B7B7B 100%);
 background: -o-linear-gradient(top,  #878787 0%,#6E6E6E 50%,#5C5C5C 51%,#7B7B7B 100%);
 background: -ms-linear-gradient(top,  #878787 0%,#6E6E6E 50%,#5C5C5C 51%,#7B7B7B 100%);
 background: linear-gradient(to bottom,  #878787 0%,#6E6E6E 50%,#5C5C5C 51%,#7B7B7B 100%);
 filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#878787', endColorstr='#7B7B7B',GradientType=0 );
}​

这里是在jsfiddle上的示例:

http://jsfiddle.net/Q3sbp/


(注意:本文为机器翻译,仅供参考。)

你的 jsfiddle 链接中有很多东西在进行。 第一个表格 select-customer-results 覆盖了另一个表格 select-customer-results 的滚动条的原因是因为第一个表格被设置为 position:fixedtop:-1px。第二个表格没有给定特定的位置,并占据了整个绝对定位的父 div。我看到你正在尝试使用一个空的 div 设置为 height:37px 来解决这个问题。这不会起作用。请参见下面 @SidharthMudgal 的答案。 - Morrowind789
2个回答

1

我注意到你的代码有一些问题。首先,它不是语义化的。 th 标签的整个目的是为了使其可机读,但你将表格拆分成没有内容的表头和没有表头的内容表格。

另一个问题是你在两个表格中使用相同的 idid 属性应该唯一地定义一个元素。如果你想对两个表格应用相同的样式,应该给它们一个类。

除了这些问题,让我们看看一些解决方案。我想到的第一个想法是在 th 表格上设置负边距,使其出现在 div 外面(确切地说,在上面)。然而,这行不通,因为 div 的整个目的是将其 overflow-y 设置为 scroll。因此,你应该将表格的代码移到 div 外面。

这是它看起来的样子,在 JSFiddle 上

我稍微修改了代码。我去掉了奇怪的 height: 35px 的 div,也删除了绝对定位。很容易将它们都绝对定位以使它们对齐。


谢谢,这个很好用 :) 我也会考虑你关于重复id的建议。这只是一个临时解决方案,我之后会优化代码... 再次感谢你的帮助! - Scott Hallauer

0
你需要在表头中去掉 position:fixed。此外,你还需移除 <div style='height:37px;'></div>,因为它会在表头后添加不必要的空白。这里是更新后的示例

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接