前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >包含合并单元格的固定表头 原

包含合并单元格的固定表头 原

作者头像
tianyawhl
发布2019-04-04 16:04:15
发布2019-04-04 16:04:15
1.1K00
代码可运行
举报
文章被收录于专栏:前端之攻略前端之攻略
运行总次数:0
代码可运行

由于是基于bootstrap基础,所以样式有些需要修改

主要用到的样式

代码语言:javascript
代码运行次数:0
运行
复制
<style>
        body {
            padding-top: 100px;
        }
		.scrollTable, .fixTable{width:100%;max-width:none;}
		.nowrap tr th, .nowrap tr td{white-space:nowrap;}
        #outtableDiv {
            padding: 0;
            height: 400px;
            overflow-y: scroll;
			overflow-x: scroll;
        }
		.fixTable thead {
            background-color: #fff; 
        }
		.fixTable{margin-bottom:0;position:absolute;
			background-color: #fff; }
    </style>

主要html结构 

代码语言:javascript
代码运行次数:0
运行
复制
<div id="outtableDiv" class="col-xs-12" >
 <table id="fixTable" class="table table-bordered fixTable nowrap">
        <thead>
        <tr>
            <th colspan="2">姓名姓名</th>
            <th>性别</th>
            <th colspan="2">身高</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
        </tr>
		<tr>
             <th>姓名姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
        </tr>
        </thead>
    </table>
    <table id="textTable" class="table table-bordered scrollTable nowrap">
        <thead>
        <tr>
            <th>姓名姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
        </tr>
		<tr>
             <th>姓名姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
			 <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>身高</th>
            <th>体重</th>
        </tr>
        </thead>
        <tbody id="testTbody">
        </tbody>
   </table>

用到的js 

代码语言:javascript
代码运行次数:0
运行
复制
<script type="text/javascript">
    
    $(document).ready(function () {
	  function duiqi(){
        var fixTh=$("#fixTable tr:last-child th");
		var contentTh=$("#textTable tr:last-child th");
		var contentTableWidth=$("#textTable").outerWidth();
		$("#fixTable").outerWidth(contentTableWidth);
		for(i=0;i<contentTh.length;i++){
			fixTh.eq(i).outerWidth(contentTh.eq(i).outerWidth());
		}
	  }
		$('#outtableDiv').scroll(function() {
		    var scrollTop=$('#outtableDiv').scrollTop();
			$("#fixTable").css("top",scrollTop);
		})
		duiqi();
		$(window).resize(function () {
		   duiqi();
        });
		
    })
</script>

缺点是每次滚动都是不停的计算scrollTop的值,把他赋给固定头的样式

$('#outtableDiv').scroll(function() {             var scrollTop=$('#outtableDiv').scrollTop();             $("#fixTable").css("top",scrollTop); })

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/05/05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档