表格列固定

<!doctype html>
<html dir="ltr" lang="zh-CN">
  <head>
    <meta charset="utf-8"/>
    <title>类似excel的滚动机制,当移动滚动条时固定table的第一行和第一列</title>
    <style type="text/css">
      #table-container{width:650px;height:250px;overflow: auto;margin: 50px;border: 1px solid #DDD;}
      #table-container table{width: 100%;border-width: 0;border-collapse: collapse;}
      #table-container table td{padding: 0;border-right: 1px solid #DDD;border-bottom: 1px solid #DDD;background: #FFF;}
      #table-container .table-top td, #table-container .table-lt td, #table-container .table-left td{background: green;}

      #table-container .table-top td:last-child{border-right: 0px solid #DDD;}
      #table-container .table-right td:last-child{border-right: 0px solid #DDD;}
      #table-container .table-right tr:last-child td{border-bottom: 0px solid #DDD;}
      #table-container .table-left tr:last-child td{border-bottom: 0px solid #DDD;}

      #table-container td div{display: inline-block;max-width: 200px;margin:5px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;text-align: left;line-height: 24px;}

      #table-container .table-lt{position: fixed;z-index:3;}
      #table-container .table-top{position: fixed;overflow:hidden;z-index:2;}
      #table-container .table-left{position: fixed;overflow:hidden;z-index:2;}
      #table-container .table-right{position: relative;z-index:1;}
      #table-container .table-mask{position: relative;}

    </style>
    <script type="text/javascript" src="jquery-1.7.1.js"></script>
  </head>
  <body>

    <div id="table-container">
      <div class="table-lt">
        <table>
          <tbody>
            <tr>
              <td>
                <div>
                  header-left-top
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="table-top">
        <div class="table-mask">
          <table>
            <tbody>
              <tr>
                <td>
                  <div>topTitle1</div>
                </td>
                <td>
                  <div>topTitle2</div>
                </td>
                <td>
                  <div>topTitle3</div>
                </td>
                <td>
                  <div>topTitle4</div>
                </td>
                <td>
                  <div>topTitle5</div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
      <div class="table-left">
        <div class="table-mask">
          <table>
            <tbody>
              <tr>
                <td>
                  <div>leftTitle1</div>
                </td>
              </tr>
              <tr>
                <td>
                  <div>leftTitle2</div>
                </td>
              </tr>
              <tr>
                <td>
                  <div>leftTitle3</div>
                </td>
              </tr>
              <tr>
                <td>
                  <div>leftTitle4</div>
                </td>
              </tr>
              <tr>
                <td>
                  <div>leftTitle5</div>
                </td>
              </tr>
              <tr>
                <td>
                  <div>leftTitle6</div>
                </td>
              </tr>
              <tr>
                <td>
                  <div>leftTitle7</div>
                </td>
              </tr>
              <tr>
                <td>
                  <div>leftTitle8</div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
      <div class="table-right">
        <table>
          <tbody>
            <tr>
              <td>
                <div>uoifdfdfuiuiouiouuoifdfdfuiuiouiouuoifdfdfuiuiouiouuoifdfdfuiuiouioud</div>
              </td>
              <td>
                <div>uoifdfdfuiuiouiou</div>
              </td>
              <td>
                <div>uoiuiufdiouiou</div>
              </td>
              <td>
                <div>uoiuiufdsfsdfiouiou</div>
              </td>
              <td>
                <div>uoiuiusiouiou</div>
              </td>
            </tr>
            <tr>
              <td>
                <div>uoiuiuiouiou</div>
              </td>
              <td>
                <div>uoifdfdfuiuiouiouuoifdfdfuiuiouiouuoifdfdfuiuiouiouuoifdfdfuiuiouiou</div>
              </td>
              <td>
                <div>uoiuiufdiouiou</div>
              </td>
              <td>
                <div>uoiuiufdsfsdfiouiou</div>
              </td>
              <td>
                <div>uoiuiusiouiou</div>
              </td>
            </tr>
            <tr>
              <td>
                <div>uoiuiuiouiou</div>
              </td>
              <td>
                <div>uoifdfdfuiuiouiou</div>
              </td>
              <td>
                <div>uoiuiufdiouiou</div>
              </td>
              <td>
                <div>uoiuiufdsfsdfiouiou</div>
              </td>
              <td>
                <div>uoiuiusiouiou</div>
              </td>
            </tr>
            <tr>
              <td>
                <div>uoiuiuiouiou</div>
              </td>
              <td>
                <div>uoifdfdfuiuiouiou</div>
              </td>
              <td>
                <div>uoiuiufdiouiou</div>
              </td>
              <td>
                <div>uoiuiufdsfsdfiouiou</div>
              </td>
              <td>
                <div>uoiuiusiouiou</div>
              </td>
            </tr>
            <tr>
              <td>
                <div>uoiuiuiouiou</div>
              </td>
              <td>
                <div>uoifdfdfuiuiouiou</div>
              </td>
              <td>
                <div>uoiuiufdiouiou</div>
              </td>
              <td>
                <div>uoiuiufdsfsdfiouiou</div>
              </td>
              <td>
                <div>uoiuiusiouiou</div>
              </td>
            </tr>
            <tr>
              <td>
                <div>uoiuiuiouiou</div>
              </td>
              <td>
                <div>uoifdfdfuiuiouiou</div>
              </td>
              <td>
                <div>uoiuiufdiouiou</div>
              </td>
              <td>
                <div>uoiuiufdsfsdfiouiou</div>
              </td>
              <td>
                <div>uoiuiusiouiou</div>
              </td>
            </tr>
            <tr>
              <td>
                <div>uoiuiuiouiou</div>
              </td>
              <td>
                <div>uoifdfdfuiuiouiou</div>
              </td>
              <td>
                <div>uoiuiufdiouiou</div>
              </td>
              <td>
                <div>uoiuiufdsfsdfiouiou</div>
              </td>
              <td>
                <div>uoiuiusiouiou</div>
              </td>
            </tr>
            <tr>
              <td>
                <div>uoiuiuiouiou</div>
              </td>
              <td>
                <div>uoifdfdfuiuiouiou</div>
              </td>
              <td>
                <div>uoiuiufdiouiou</div>
              </td>
              <td>
                <div>uoiuiufdsfsdfiouiou</div>
              </td>
              <td>
                <div>uoiuiusiouiou</div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>

    </div>

    <script type="text/javascript">
      $(document).ready(function(argument) {
        var container = $("#table-container"),
            ltTable = container.find(".table-lt"),
            topTable = container.find(".table-top"),
            leftTable = container.find(".table-left"),
            rightTable = container.find(".table-right"),

            containerWidth = 0,
            containerHeight =0,

            ltTableWidth = 0,
            ltTableHeight = 0,

            widthJson = {},

            timerLT = null;

        ltTableWidth = ltTable.width();
        ltTable.width(ltTableWidth);
        topTable.css("marginLeft",ltTableWidth);
        leftTable.width(ltTableWidth);
        rightTable.css("marginLeft",ltTableWidth);
        ltTableHeight = ltTable.height();
        leftTable.css("marginTop",ltTableHeight+"px");
        rightTable.css("marginTop",ltTableHeight+"px");

        containerHeight = container.height();
        containerWidth = container.width();
        topTable.width(containerWidth - ltTableWidth - (container.innerWidth() - container[0].clientWidth));
        leftTable.height(containerHeight - ltTableHeight - (container.innerHeight() - container[0].clientHeight));

        // figure out the width of each DIV in TD  --start
        function setDivWidth(obj){
          $(obj).find("div").each(function(index){
            if(!widthJson[index]){
              widthJson[index] = 0;
            }
            if(widthJson[index]<$(this).width()) {
              widthJson[index] = $(this).width();
            }
          });
        }
        topTable.find("tr").each(function(){
          setDivWidth(this);
        });
        rightTable.find("tr").each(function(){
          setDivWidth(this);
        });

        topTable.find("tr:first div").each(function(index){
          $(this).width(widthJson[index]);
        });
        rightTable.find("tr:first div").each(function(index){
          $(this).width(widthJson[index]);
        });
        // figure out the width of each DIV in TD  --end

        container.scroll(function(){
          var currentScrollTop = $(this).scrollTop(),
              currentScrollLeft = $(this).scrollLeft();
          topTable.find(".table-mask").css("left", -currentScrollLeft+"px");
          leftTable.find(".table-mask").css("top", -currentScrollTop+"px");

        });

        $(document).scroll(function(){
          var currentScrollTop = $(this).scrollTop(),
              currentScrollLeft = $(this).scrollLeft();
          ltTable.css("marginTop", -currentScrollTop+"px");
          ltTable.css("marginLeft", -currentScrollLeft+"px");
          topTable.css("marginTop", -currentScrollTop+"px");
          topTable.css("marginLeft", ltTableWidth - currentScrollLeft+"px");
          leftTable.css("marginTop", ltTableHeight - currentScrollTop+"px");
          leftTable.css("marginLeft", -currentScrollLeft+"px");

        });

        // for IE, when window‘s scrollbar is moved, keep ltTable,leftTable,topTable in the correct position
        $(window).resize(function(){
          $(document).scroll();
        });

        //reset the width of topTable and the height of leftTable when container‘s size is changed, if container‘s size is fixed, you can skip the code below.
        timerLT = setInterval(function(){
          if(containerWidth != container.width() || containerHeight != container.height()){
            topTable.width(container.width() - ltTableWidth - (container.innerWidth() - container[0].clientWidth));
            leftTable.height(container.height() - ltTableHeight - (container.innerHeight() - container[0].clientHeight));

            containerWidth = container.width();
            containerHeight = container.height();

            container.scroll();// for IE

          };

        },0);

      });
    </script>

  </body>
</html>

  

时间: 2024-10-06 14:14:18

表格列固定的相关文章

表格固定列固定表头插件结合css3

/*20170628固定表格列*/ ;(function($){ $.fn.Scrollhanle=function(set){ var opt=$.extend({ size:3 },set||{}); return this.each(function(){ var sleft=$(this); var $color='#FFF'; var $headtr=sleft.find('thead>tr'); var $tbody=sleft.find('tbody tr'); sleft.scr

CSS简单table首列固定

日前公司业务需求,将几个页面Table(也有一个页面几个table的情况)首列固定.网上查找资料最简便的做法是,将固定列(或者行)与滑动数据分别放在两个table里.因为我的需求是在 原有的table上进行编辑修改,不宜大幅度动原有代码所以没有采用. 考虑到时间成本的问题最终解决代码如下.值得注意的地方有几点: 第一,使用 css experssion动态表达式取值 加上eval绑定为较规范写法(也有说可以兼容IE6). 第二,必须使用兼容视图(IE)浏览,否则Freezingcol中left属

表格列点击排序

实现点击表格列实现排序的效果,按照一列一列的排序 代码如下: 排序代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> table{ border-collapse: collapse; } th,td{ height: 30px; width: 8

FineUIMvc随笔 - 动态创建表格列

声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 用户需求 用户希望实现动态创建表格列,在 WebForms 中,我们通过在 Page_Init 中创建列来实现: 但是在 MVC 中,如果还想着 WebForms 的那一套,想着怎么才能在 Controller 中访问 View 中的表格控件,这是行不通的. 我曾写过一个系列文章<ASP.NET MVC快速入门(MVC5+EF6)>,开篇就讲到了 MVC 中的页面的生成流程: 这个页面之所以能够呈现在我们眼前,经历了三个主

表格列宽怎么设置都无效?(td内容自动换行问题)

有次设置表格列宽时,显示的实际宽度与设置的宽度不一致,且无规律变化.最后,在查找资料后发现,可能是td内容 自动换行 的问题.因为表格中的内容有中文.英文字母.还有数字,以及三者的各种组合,所以换行的方式不一致,导致列宽变化.(个人分析,不妥之处请拍砖.) 所以添加以下css可以解决问题: table{ word-break:break-all ; word-wrap:break-word } 说明: (1) word-break:break-all:截断单词进行换行. (2) word-wra

1_表格列的控制.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

jQuery拖动调整表格列宽度-resizableColumns

实现鼠标可拖动调整表格列宽度 如图: 一.引入文件: <script src="/js/jquery-1.8.0.min.js" type="text/javascript"></script> <script src="/js/store.js" type="text/javascript"></script> <script src="/js/jquery.r

jQuery表格列宽可变,兼容firfox

本demo使用jQuery包,实现表格列宽可拖拽功能,并实现页面reset时的重新布局.使用jQuery,方便函数的调用,给要处理的表格添加id 后,直接调用$("#id").movedTh()即可方便实现,修改了firfox的兼容性. 代码如下: [javascript] view plaincopy <html> <head> <meta http-equiv="Content-Type" content="text/ht

动态增加,删除表格行并按照表格列排序

接着上一篇博文,解决上一篇博文最后遗留的瑕疵,同时,给表格列添加了排序功能. 解决掉的问题 1 乱码的问题 2 删除方法更加简便,省去获取父节点id的代码 3 点击第一行的add按钮后,下行table行变宽问题 <html> <head> <title>Table</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"&g