2013-05-09 53 views
0

我試圖在滾動頁面的同時製作粘貼表格。目前我有兩張桌子,但是當我滾動頁面時,只有第一張桌子移動了大約10px;並停下來。這是我的JSFiddle,我的桌子根本不動。任何人都可以建議我,我做錯了什麼?滾動時將整張桌子粘到原來的位置

HTML:

<div id="header"> 
    header 
</div> 

<table class="table_filter_data filter_table" cellpadding='0' cellspacing='0' width='100%'> 
    <tr> 
     <td> 
      table_1 
     </td> 
    </tr> 
</table> 
     <table class="table_filter_data filter_table" cellpadding='0' cellspacing='0' width='100%'> 
    <tr> 
     <td> 
      table_2 
     </td> 
    </tr> 
</table> 

的jQuery:

<script type="text/javascript"> 
      function UpdateFilterHeaders() { 
       $("div.div_filter_table").each(function() {     
       var originalTable = $(".tableFloatingHeaderOriginal", this); 
       var floatingTable = $(".tableFloatingHeader", this); 

       var offset = $(this).offset(); 
       var scrollTop = $(window).scrollTop(); 

       if ((scrollTop > offset.top) && (scrollTop < offset.top + $(this).height())) { 
        floatingTable.css("visibility", "visible"); 
        originalTable.css("visibility", "hidden"); 
        floatingTable.css("top", Math.min(scrollTop - offset.top, $(this).height() - floatingTable.height()) + "px"); 

        // Copy cell widths from original header 
        $(floatingTable).each(function(index) { 
         var cellWidth = $(originalTable).eq(index).css('width'); 
         $(this).css('width', cellWidth); 
        }); 

        // Copy row width from whole table 
        floatingTable.css("width", $(this).css("width")); 
       } 
       else { 
        originalTable.css("visibility", "visible"); 
        floatingTable.css("visibility", "hidden"); 
        floatingTable.css("top", "0px"); 
       } 
       }); 
      } 

      $(document).ready(function() { 

       $("table.filter_table").each(function() { 
        $(this).wrap("<div class=\"div_filter_table\" style=\"position:relative\"></div>"); 

        var originalTable = $(this);     
        var clonedTable = originalTable.before(originalTable.clone() 
        .addClass("tableFloatingHeader") 
        .css("position", "absolute") 
        .css("top", "0px") 
        .css("left", $(this).css("margin-left")) 
        .css("visibility", "hidden")); 

        originalTable.addClass("tableFloatingHeaderOriginal"); 
       }); 
       UpdateFilterHeaders(); 
       $(window).scroll(UpdateFilterHeaders); 
       $(window).resize(UpdateFilterHeaders); 
      }); 
     </script> 

CSS:

body {height:2000px;} 
.table_filter_data{ margin: 0px ; padding: 0px; padding-left: 3px; padding-top: 10px;} 
.table_filter_data > tbody > tr > td{ padding: 2px; text-align: left; font-size: 0.9em;} 
.table_filter_data > tbody > tr > td.title{ text-align: right;}    
.table_filter_data > tbody > tr > td > input{ font-size: 0.9em;} 
.table_filter_data > tbody > tr > td > select{ font-size: 0.9em;} 
#header {height: 100px; border: 1px solid gray} 
+0

請在你的帖子中包含你的代碼:http://meta.stackexchange.com/questions/84342/answer-that-only-contains-a-link-to-jsfiddle – Lowkase 2013-05-09 12:42:05

+0

你能不能請一把小提琴? – 2013-05-09 12:51:57

+0

@RupamDatta看看描述,它是一個鏈接。 http://jsfiddle.net/StaG8/3/ – DeividasJJ 2013-05-09 12:53:49

回答

1

我會建議使用position: fixed對於要被粘表。

相關問題