利用CSS+DIV來固定table的行和列

來源:互聯網
上載者:User

通過CSS和DIV實現一個表格出橫縱向捲軸,並且讓指定的列固定。多餘的話就不說了,代碼如下。

<html>   
<head>   
<title></title>   
<style type="text/css">   
.Freezing {   
    z-index: 10;    
    position: relative;    
    top: expression(this.offsetParent.scrollTop)   
}   
.FreezingCol {   
    z-index: 1;    
    left: expression(document.getElementById("freezingDiv").scrollLeft);    
    position: relative   
}   
</style>   
</head>   
<body>   
  
<div id="freezingDiv" style="overflow:auto; width:400px; height:155px">   
  <table cellspacing="0" cellpadding="4" rules="all" bordercolor="#3366CC" border="1" id="DataGrid1" style="background-color:White;border-color:#3366CC;border-width:1px;border-style:None;width:900px;border-collapse:collapse;">   
 
    <tr class="Freezing" style="color:#CCCCFF;background-color:#003399;font-weight:bold;">
        <td class="FreezingCol">ID</td>   
        <td class="FreezingCol">姓名</td>   
        <td>年齡</td>   
        <td>電話</td>   
        <td>性別</td>   
        <td>住址</td>   
    </tr>   
       
    <tr style="color:#003399;background-color:White;">   
        <td class="FreezingCol">   
         <span>1</span>    
        </td>   
        <td class="FreezingCol">   
        <span>張三</span>    
        </td>   
        <td>29</td>   
        <td>0000000000000</td>   
        <td>男</td>   
        <td>北京</td>   
   </tr>
   <tr style="color:#003399;background-color:White;">   
        <td class="FreezingCol">   
           <span>1</span>    
        </td>   
        <td class="FreezingCol">   
          <span>李四</span>    
        </td>   
        <td>29</td>   
        <td>0000000000000</td>   
        <td>男</td>   
        <td>上海</td>   
   </tr>
   <tr style="color:#003399;background-color:White;">   
        <td class="FreezingCol">   
           <span>1</span>    
        </td>   
        <td class="FreezingCol">   
          <span>王五</span>    
        </td>   
        <td>50</td>   
        <td>0000000000000</td>   
        <td>男</td>   
        <td>上海</td>   
   </tr>
   <tr style="color:#003399;background-color:White;">   
        <td class="FreezingCol">   
           <span>1</span>    
        </td>   
        <td class="FreezingCol">   
          <span>趙六</span>    
        </td>   
        <td>29</td>   
        <td>0000000000000</td>   
        <td>男</td>   
        <td>上海</td>   
   </tr> 
   <tr style="color:#003399;background-color:White;">   
        <td class="FreezingCol">   
           <span>1</span>    
        </td>   
        <td class="FreezingCol">   
          <span>趙七</span>    
        </td>   
        <td>29</td>   
        <td>0000000000000</td>   
        <td>男</td>   
        <td>上海</td>   
   </tr>        
  </table>   
</div>   
  
</body>   
</html>

這種方式在資料很多的時候,拖動頁面捲軸的時候很慢。

轉自:http://blog.csdn.net/xuyusong/article/details/6618847

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.