话不多说,跟这小编来一起看下吧
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>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,同时也希望多多支持!
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《中央民族乐团-古筝传奇》1CD[MP3][253.1MB]
- 江玲.1989-这样你才爱我,是吗?【新作有声】【WAV+CUE】
- 郭美美.2010-我是郭美美【华纳】【WAV+CUE】
- 黄心懋1991-轻忧郁【滚石】【WAV+CUE】
- 群星.2000-大地金曲世纪回顾2CD【大地】【WAV+CUE】
- 王菲.1996-浮躁(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 群星.2022-传家电视剧原声带【东阳欢娱】【FLAC分轨】
- 《摇滚教父 伍佰黄金精选 2CD》 [WAV+CUE][1GB]
- 《凤凰传奇 我从草原来》[WAV/分轨][400MB]
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]