利用js+css+html实现固定table的列头不动

 更新时间:2016年12月08日 16:54:38   作者:扯  
本文分享了利用js+css+html实现固定table的列头不动的实例代码。小编认为具有很好的参考价值,感兴趣的朋友可以看下

话不多说,跟这小编来一起看下吧

1、CSS

<style type="text/css">
  #scroll_head {
    position: absolute;
    display: none;
  }
</style>

2、Javascript

<script type="text/javascript">
  //该函数在上面一个table数据加载完成后调用
  //把表头的宽度设置到会滚动的页头去
  var copyWidth = function () {
    var b = $('#data_tbody').prev().find('tr:last').find('th');
    var c = $('#scroll_head').find('tr:last').find('th');
    for (var i = 0; i < b.length; i++) {
      var newWith = b.eq(i).width();
      if ($.browser.msie) {
        newWith += 1;
      }
      c.eq(i).width(newWith);
    }
  }
  $(function () {
    $(window).scroll(function () {
      if ($('#data_tbody').length > 0) {
        var thead = $('#data_tbody').prev();
        var thOffset = thead.offset();
        var scTop = $(window).scrollTop(); //滚动条相对top的位置
        if (scTop > thOffset.top) {  //滚动条滚到thead及以下的位置,用临时的thead代替显示
          $('#scroll_head').css('display', 'block');
          $('#scroll_head').offset({ top: scTop, left: thOffset.left });
        }
        else { //滚动条滚到thead上的位置,用table的原始thead显示
          $('#scroll_head').css('display', 'none');
        }
      }
    });
  });
</script>

3、Html内容

<div id="data_div">
  <table>
    @*thead内容及样式同scroll_head中的thead*@
    @*thead使用深背景色,避免滚动时和tbody内容重叠显示*@
    <thead> 
      <tr>
        @*一级标题*@
        <th class="tt1" colspan="2">一级1</th>
        <th class="tt2" colspan="5">一级2</th>
        <th class="tt3" colspan="6">一级3</th>
      </tr>
      <tr>
        @*二级标题*@
        <th style="width: 23px;">二级11</th>
        <th style="width: 36px;">二级12</th>
        <th class="tt" style="width: 40px;">二级21</th>
        <th class="tt" style="width: 30px;">二级22</th>
        <th class="tt" style="width: 30px;">二级23</th>
        <th class="tt" style="width: 30px;">二级23</th>
        <th class="tt" style="width: 30px;">二级24</th>
        <th class="tt" style="width: 30px;">二级25</th>
        <th class="tt" style="width: 30px;">二级31</th>
        <th class="tt" style="width: 30px;">二级32</th>
        <th class="tt" style="width: 30px;">二级33</th>
        <th class="tt" style="width: 30px;">二级33</th>
        <th class="tt" style="width: 30px;">二级34</th>
        <th class="tt" style="width: 30px;">二级35</th>
        <th class="tt" style="width: 30px;">二级36</th>
      </tr>
    </thead>
    <tbody id="data_tbody">
      数据内容,在数据加载完成后调用copyWidth()函数解决兼容性
    </tbody>
  </table>
</div>
<div id="scroll_head" style="display:block; top: 168px; left: 0px; position: relative;">
  <table width="100%">
    <thead> @*thead使用深背景色,避免滚动时和tbody内容重叠显示*@
      <tr>
        @*一级标题*@
        <th class="tt1" colspan="2">一级1</th>
        <th class="tt2" colspan="5">一级2</th>
        <th class="tt3" colspan="6">一级3</th>
      </tr>
      <tr>
        @*二级标题*@
        <th style="width: 23px;">二级11</th>
        <th style="width: 36px;">二级12</th>
        <th class="tt" style="width: 40px;">二级21</th>
        <th class="tt" style="width: 30px;">二级22</th>
        <th class="tt" style="width: 30px;">二级23</th>
        <th class="tt" style="width: 30px;">二级23</th>
        <th class="tt" style="width: 30px;">二级24</th>
        <th class="tt" style="width: 30px;">二级25</th>
        <th class="tt" style="width: 30px;">二级31</th>
        <th class="tt" style="width: 30px;">二级32</th>
        <th class="tt" style="width: 30px;">二级33</th>
        <th class="tt" style="width: 30px;">二级33</th>
        <th class="tt" style="width: 30px;">二级34</th>
        <th class="tt" style="width: 30px;">二级35</th>
        <th class="tt" style="width: 30px;">二级36</th>
      </tr>
    </thead>
  </table>
</div>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,同时也希望多多支持脚本之家!

相关文章

  • JavaScript实现将数组数据添加到Select下拉框的方法

    JavaScript实现将数组数据添加到Select下拉框的方法

    这篇文章主要介绍了JavaScript实现将数组数据添加到Select下拉框的方法,涉及javascript数组操作及页面元素动态赋值的相关技巧,需要的朋友可以参考下
    2015-08-08
  • 动态加载脚本提升javascript性能

    动态加载脚本提升javascript性能

    动态加载脚本可以有效提升javascript性能,下面有个不错的示例,大家可以参考下
    2014-02-02
  • JavaScript数组reduce()方法的语法与实例解析

    JavaScript数组reduce()方法的语法与实例解析

    js函数中有三个在特定场合很好用的函数:reduce(),map(),filter(),这篇文章主要给大家介绍了关于JavaScript数组reduce()方法的相关资料,需要的朋友可以参考下
    2021-07-07
  • js时钟翻牌效果实现代码分享

    js时钟翻牌效果实现代码分享

    这篇文章主要介绍了javascript时钟翻牌效果的实现,效果非常酷炫,实现步骤也很简单,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-08-08
  • 微信小程序如何实现五星评价功能

    微信小程序如何实现五星评价功能

    这篇文章主要介绍了微信小程序如何实现五星评价功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • js改变img标签的src属性在IE下没反应的解决方法

    js改变img标签的src属性在IE下没反应的解决方法

    在Chrome FF里都能改变成功,但在IE下却不行,网上搜了半天,大概了解了,这个是IE的一个bug,具体的解决方法如下,有类似问题的朋友可以参考下哈,希望对大家有所帮助
    2013-07-07
  • JS中去掉array中重复元素的方法

    JS中去掉array中重复元素的方法

    这篇文章主要介绍了JS中去掉array中重复元素的方法,需要的朋友可以参考下
    2017-05-05
  • webpack打包后直接访问页面图片路径错误的解决方法

    webpack打包后直接访问页面图片路径错误的解决方法

    这篇文章主要给大家介绍了在webpack打包后直接访问页面图片路径错误的解决方法,文中介绍的非常详细,对遇到这个问题的朋友们具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-06-06
  • Javascript中级语法快速入手

    Javascript中级语法快速入手

    本文是一篇关于Javascript的中级进阶总结。小编希望大家能够在30分钟之内阅读完并认真体会,多敲代码,多总结,毕竟Javascript本身并不难。
    2016-07-07
  • javascript表单是否为空验证方法

    javascript表单是否为空验证方法

    表单验证在很多地方都可以用到,本文详细的介绍了javascript表单是否为空验证方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03

最新评论