2015-01-14 98 views
4

我正在使用粘性表格標題。當用戶滾動到表格和表格標題上時,我希望表格標題粘貼到頁面的頂部並跟隨用戶。我目前的問題是,當我將thead position更改爲absolute時,標題會丟失寬度。我如何保留(或重置?)標題寬度以符合我的列?如何在更改位置時保留表格標題寬度

注意:我不能使用任何常量,寬度可以根據數據更改。

注意:我知道插件,但是我在不使用插件的情況下編碼。

注意:我正在使用引導CSS。

HTML

    <div class="table-responsive" style="overflow:auto"> 
        <table id="table" class="table tablesorter table-striped table-hover"> 
        <thead id='tableHeader' style="background-color:white;"> 
         <tr> 
          <th class='header'>Column1 Head</th> 
          <th class='header'>Column2 Head</th> 
          <th class='header'>Column3 Head</th> 
          <th class='header'>Column4 Head</th> 
          <th class='header'>Column5 Head</th> 
          <th class='header'>Column6 Head</th> 
          <th class='header'>Column7 Head</th> 
         </tr> 
        </thead> 
         <tbody id='tableBody'> 
         </tbody> 
        </table> 
        </div> 

的Javascript

 //Window scroll event listener to fix table headers 
    var tableHeaderTop = $("#tableHeader").offset().top; 
    $(window).scroll(function() { 
     if(tableHeaderTop - $(window).scrollTop() <= 0){ 
      console.log('scroll below header'); 
      $('#tableHeader').css({ 
       position:'absolute', 
       top: $(window).scrollTop() - $("#top").height() -15 
      }); 
     }else{ 
      console.log('scroll above header'); 
      $('#tableHeader').css({ 
       position:'static', 
      }); 
     } 
    }); 

enter image description here

enter image description here

+0

你介意提供的css?如果沒有它,解決方案可能不會立即明確(或試圖複製它)。 – wahwahwah

+0

@wahwahwah,謝謝你的迴應。我正在使用bootstrap。我是否應該在我的問題中包括這個? –

+1

我會將bootstrap作爲標籤添加到您的問題中,除非有人已經爲您解答。 – wahwahwah

回答

3

一個O請在設置絕對樣式之前詢問列,然後在將樣式應用於標題後恢復該寬度。事情是這樣的: 小提琴這裏:http://jsfiddle.net/mn76ujsu/3/

//Window scroll event listener to fix table headers 
var tableHeaderTop = $("#tableHeader").offset().top; 
$(window).scroll(function() { 
    if(tableHeaderTop - $(window).scrollTop() <= 0){ 
     console.log('scroll below header'); 
     var col1Width = $('#column1').width(); //Find width before change 
     var col2Width = $('#column2').width(); 
     var col3Width = $('#column3').width(); 
     $('#tableHeader').css({ 
      position:'absolute', 
      top: $(window).scrollTop() - $("#top").height() -0 
     }); 

     $('.column1Value').width(col1Width); 
     $('#column1').width(col1Width); 
     $('.column2Value').width(col2Width); 
     $('#column2').width(col2Width); 
     $('.column2Value').width(col3Width); 
     $('#column3').width(col3Width); 
    }else{ 
     console.log('scroll above header'); 
     $('#tableHeader').css({ 
      position:'static', 
     }); 
    } 
}); 

使用HTML,包括appropirate類和ID:

<div class="table-responsive" style="overflow:auto"> 
       <table id="table" class="table tablesorter table-striped table-hover"> 
       <thead id='tableHeader' style="background-color:white;"> 
        <tr> 
         <th id="column1" class='header'>Column1 Head</th> 
         <th id="column2" class='header'>Column2 Head</th> 
         <th id="column3" class='header'>Column3 Head</th> 

        </tr> 
       </thead> 
        <tbody id='tableBody'> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 


         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
         <tr> 
          <td class="column1Value">Some text</td> 
          <td class="column2Value">Some text</td> 
          <td class="column3Value">Some text</td> 
         </tr> 
        </tbody> 
       </table> 
       </div> 
+0

這有我面對同樣的問題 - http://jsfiddle.net/mn76ujsu/1/使結果非常寬,然後向下滾動,所有標題移動到一邊,失去寬度。感謝您的嘗試。 –

+0

這真棒,但如果它在DIV容器內呢?滾動條在DIV內部? – SearchForKnowledge

+2

約旦。您還需要指定標題的寬度,以便它們不會移位。我用新的小提琴(http://jsfiddle.net/mn76ujsu/3/)和javascript更新了我的答案。 –