javascript - Stick whole table to its original position while scrolling -


i'm trying make sticky table while scrolling page. i'm having 2 tables 1 after another, when i'm scrolling page, first table moves 10px; , stops. here jsfiddle , tables don't move @ all. anyone, can advice me, i'm doing wrong?

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 original header                     $(floatingtable).each(function(index) {                         var cellwidth = $(originaltable).eq(index).css('width');                         $(this).css('width', cellwidth);                     });                      // copy row width 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} 

i suggest using position: fixed tables want sticky.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -