Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

96 linhas
4.6 KiB

  1. window.onload = function(){
  2. var sourceTable = $("#table-data");//table id 获取表对象
  3. var sourceTableHead = $("#table_head");//table thead tr id 获取表头thead
  4. var headHeight = sourceTableHead.outerHeight();//table thead tr height 获取资源表的宽度
  5. //copy table and thead html tag from source table,
  6. $('#table-data').append('<div id="shelter"><table id="fixed_table" class="table table-bordered table-hover" style="margin-left: 1px;"><thead></thead></table></div>'); //重建一个table添加到body中
  7. //only set top and left,beacuse i need the top bar can scroll left
  8. $("#shelter").css({'height':headHeight,'position':'fixed','top':'0','left':'25'}); // 设置目标表格的高度等于源表格的高度,高度是一定的
  9. // 克隆源表格的头部并设置目标表格的id=fix_head
  10. var targetHead = sourceTableHead.clone();
  11. targetHead.attr('id','fix_head');
  12. targetHead.appendTo('#fixed_table thead');
  13. targetHead.find('input').attr('onclick', 'alterCheck(this)');
  14. // 根据源表的宽度和高度设置目标的宽和高
  15. $("#table_head th").each(function(index,value){
  16. var tempWidth = $(value).outerWidth();
  17. // var tempHeight = $(value).outerHeight();
  18. $("#fixed_table th").eq(index).css({'width':tempWidth + "px", 'height':35+"px"});
  19. });
  20. var myThead = $('#shelter');
  21. $(window).resize(function()
  22. {
  23. // 当窗口变化时重复以上步骤
  24. var sourceTable = $("#table-data");//table id
  25. var sourceTableHead = $("#table_head");//table thead tr id
  26. var headHeight = sourceTableHead.outerHeight();//table thead tr height
  27. $('#table-data').append('<div id="shelter" class="shelter removeShelter"><table id="fixed_table" class="table table-bordered table-hover" style="margin-left: 1px;"><thead></thead></table></div>');
  28. //only set top and left,beacuse i need the top bar can scroll left
  29. $("#shelter").css({'height':headHeight,'position':'fixed','top':'0','left':'25'});
  30. var sourceTableWidth = sourceTable.outerWidth(); //get source table width
  31. $("#fixed_table").css({'width':sourceTableWidth+"px"});
  32. $('.removeShelter').remove();
  33. // 窗口变化时获取源表格的头部,遍历头部的th标签,设置目标表格的头的宽度
  34. $("#table_head th").each(function(index,value){
  35. var tempWidth = $(value).outerWidth();
  36. // var tempHeight = $(value).outerHeight();
  37. $("#fixed_table th").eq(index).css({'width':tempWidth + "px",'height':35+"px"});
  38. });
  39. });
  40. $('#table_head th').bind('DOMNodeInserted', function(e){
  41. // 当窗口变化时重复以上步骤
  42. var sourceTable = $("#table-data");//table id
  43. var sourceTableHead = $("#table_head");//table thead tr id
  44. var headHeight = sourceTableHead.outerHeight();//table thead tr height
  45. $('#shelter').remove();
  46. $('#table-data').append('<div id="shelter" class="shelter"><table id="fixed_table" class="table table-bordered table-hover" style="margin-left: 1px;"><thead></thead></table></div>');
  47. //only set top and left,beacuse i need the top bar can scroll left
  48. $("#shelter").css({'height':headHeight,'position':'fixed','top':'0','left':'25'});
  49. var targetHead = sourceTableHead.clone();
  50. targetHead.attr('id','fix_head');
  51. targetHead.appendTo('#fixed_table thead');
  52. targetHead.find('input').attr('onclick', 'alterCheck(this)');
  53. var sourceTableWidth = sourceTable.outerWidth(); //get source table width
  54. $("#fixed_table").css({'width': sourceTableWidth + "px"});
  55. // $('.shelter').remove();
  56. // 获取源表格的头部,遍历头部的th标签,设置目标表格的头的宽度
  57. $("#table_head th").each(function(index,value){
  58. var tempWidth = $(value).outerWidth();
  59. var tempHeight = $(value).outerHeight();
  60. $("#fixed_table th").eq(index).css({'width':tempWidth+"px",'height':tempHeight+"px"});
  61. });
  62. $("#shelter").hide();
  63. });
  64. // 先将目标表头隐藏d
  65. $("#shelter").hide();
  66. // 窗口滚动时触发的事件
  67. $(window).scroll(function(){
  68. //scroll left method
  69. var sl=-Math.max($('body').scrollLeft(), $('document').scrollLeft());
  70. if(isNaN(sl)){
  71. sl = - $(window).scrollLeft();
  72. }
  73. $('#shelter').css("left", 25 + sl +'px');
  74. var scroll_top = $(document).scrollTop() - sourceTable.offset().top;
  75. //control show or hide
  76. if (scroll_top > 0) {
  77. $('#shelter').show();
  78. }else {
  79. $('#shelter').hide();
  80. }
  81. });
  82. };