如何用css实现 表格第一列固定 其余内容横向滚动

最好是能给这列td或th单独定义css或js控制,这样比较好,有没有这种定义的呀... 最好是能给这列td或th单独定义css或js控制,这样比较好,有没有这种定义的呀 展开
 我来答
miniapprUqDD5zloDjsH
2020-04-12
知道答主
回答量:5
采纳率:0%
帮助的人:3019
展开全部

可以参考此样例:

.table{width:100%;overflow-x: scroll;background-color:#7c95b5;}.fixedTable{width:160%;text-align: center;color:#fff;font-size:14px; border-collapse:collapse;}.fixedTable tr{line-height: 30px;border:1px solid #fff;}.fixedTable tr:first-child{height:40px;line-height: 40px;background-color:cadetblue;}.fixedTable td:first-child{position:absolute;width:105px;background-color:cadetblue;border:1px solid #fff;margin:-1px 0px 0px -1px;}.fixedColumn{width:99px;}

<html>
<head>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,user-scalable=no">
<meta charset="utf-8">
<title>使用css固定table第一列</title>
<link href="css/table.css" rel="stylesheet"/>
</head>
<body>
<div class="table">
<table class="fixedTable" border="1">
<tr><td>固定列</td><td class="fixedColumn"></td><td>第一列</td><td>第二列</td><td>第三列</td><td>第四列</td><td>第五列</td><td>第六列</td><td>第七列</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>固定列</td><td class="fixedColumn"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
</table>
</div>
</body></html>

View Code

 

适用移动端

转载于:www.mdcode.cn

七鑫易维信息技术
2024-09-02 广告
Play Video 七鑫易维是致力于机器视觉和人工智能领域的高新科技企业,迄今已专注眼球追踪技术的研发、创新与应用超过14年,拥有完全自主知识产权,全球专利总量655余项。 作为眼球追踪技术领域的全球知名品牌,七鑫易维的产品体系覆盖眼动分... 点击进入详情页
本回答由七鑫易维信息技术提供
轻狂大本营
2018-06-21 · 超过17用户采纳过TA的回答
知道答主
回答量:145
采纳率:0%
帮助的人:17.3万
展开全部

<!DOCTYPE html><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <meta charset="utf-8" />    <style type="text/css">        * {            margin: 0;            padding: 0;        }         #table {            position: relative;        }             #table th:first-child, #table th:nth-child(2), #table th:last-child, #table tr > td:first-child, #table tr > td:nth-child(2), #table tr > td:last-child {                background-color: #ff0000;                position: fixed;                z-index: 2;            }    </style>    <script src="../Scripts/jq/jquery-2.1.4/jquery-2.1.4.js"></script>    <script type="text/javascript">        $(function () {            if ($("#table>thead>tr>th").length > 5) {                //设置固定列的th和td的with都为最长的那个td的长度                var tdWith = 0;                $("#table tr>td:first-child").each(function (index, item) {                    if ($(item).width() > tdWith) {                        tdWith = $(item).width();                    }                });                $("#table th:first-child,#table tr>td:first-child").width(tdWith);                tdWith = 0;                $("#table th:nth-child(2),#table tr>td:nth-child(2)").each(function (index, item) {                    if ($(item).width() > tdWith) {                        tdWith = $(item).width();                    }                });                $("#table th:nth-child(2),#table tr>td:nth-child(2)").width(tdWith);                tdWith = 0;                $("#table th:last-child,#table tr>td:last-child").each(function (index, item) {                    if ($(item).width() > tdWith) {                        tdWith = $(item).width();                    }                });                $("#table th:last-child,#table tr>td:last-child").width(tdWith);                //设置外面div的with                var div_abroadWith = 0;                $("#table>tbody>tr:first-child>td").each(function (index, item) {                    if (index < 5) {                        div_abroadWith += $(item).width();                    } else {                        return false;                    }                });                //设置table的with                var tableWith = 0;                $("#table>tbody>tr:first-child>td").each(function (index, item) {                    tableWith += $(item).width();                });                $("#table").width(tableWith);                div_abroadWith += $("#table th:last-child").width();                $("#div_abroad").width(div_abroadWith);                //设置固定列的偏移量                $("#table th:first-child,tr>td:first-child").css("left", $("#div_abroad").offset().left);                $("#table th:nth-child(2),tr>td:nth-child(2)").css("left", $("#div_abroad").offset().left + $("#table td:first-child").width());                $("#table th:last-child,tr>td:last-child").css("left", $("#div_abroad").offset().left + $("#div_abroad").width() - $("#table td:last-child").width());                 //因为第一,二和最后一个设置定位了,所以table里缺少了3列造成滚动条不足,所以要为table补充3列,补充位置有要求,必须在第二列的后面补上第一,第二列。这两列的宽度和原先的保持一致                //最后一列的上面要补充一列,宽度和最后一列保持一致。                var $td = $("<th style='width:" + $("#table th:first-child").width().toString() + "px'></th><th style='width:" + $("#table th:nth-child(2)").width().toString() + "px'></th>");                $("#table th").eq(2).before($td);                $td = "<td style='width:" + $("#table th:first-child").width().toString() + "px'></td><td style='width:" + $("#table th:nth-child(2)").width().toString() + "px'></td>";                $("#table tbody>tr").each(function (index, item) {                    $(item).find("td").eq(2).before($($td));                });                $td = $("<td style='width:" + $("#table th:last-child").width().toString() + "px'></td>");                $("#table tbody>tr>td:last-child").before($td);            }        });    </script></head><body>    <div id="div_abroad" style="overflow-x: auto;width:300px;margin:50px auto;background:#4cff00">        <table id="table" style="border-collapse: collapse" borderColor=#000000 height=40 cellPadding=1 border=1>            <thead>                <tr>                    <th>列1</th>                    <th>列2</th>                    <th>列3</th>                    <th>列4</th>                    <th>列5</th>                    <th>列6</th>                    <th>列7</th>                    <th>列8</th>                    <th>列9</th>                    <th>列10</th>                </tr>            </thead>            <tbody>                <tr>                    <td>111111111111111111111111</td>                    <td>222222222</td>                    <td>33333333</td>                    <td>44444</td>                    <td>555555555555555555555</td>                    <td>66666666</td>                    <td>777777</td>                    <td>88888888</td>                    <td>9999999</td>                    <td>10959</td>                </tr>                <tr>                    <td>111111111111111</td>                    <td>22222222222</td>                    <td>333333</td>                    <td>444444444444</td>                    <td>5555555555555555</td>                    <td>666666666666</td>                    <td>777777</td>                    <td>88888888888888888</td>                    <td>999999999999</td>                    <td>109555559</td>                </tr>                <tr>                    <td>111111111111111</td>                    <td>222222222222222222222</td>                    <td>333333333333</td>                    <td>4444444</td>                    <td>555555555555555555555</td>                    <td>66666666</td>                    <td>777777</td>                    <td>88888888</td>                    <td>9999999</td>                    <td>109555999995559</td>                </tr>                <tr>                    <td>111111111111111</td>                    <td>22222222222</td>                    <td>333333eeee33</td>                    <td>44444</td>                    <td>555555555555555555555</td>                    <td>66666666</td>                    <td>777wwww777</td>                    <td>88888eee888</td>                    <td>9999ww999</td>                    <td>1095555559</td>                </tr>                <tr>                    <td>111111111111111</td>                    <td>22222222222222222222222222</td>                    <td>333ww33333</td>                    <td>44sss444</td>                    <td>555555555555555555555</td>                    <td>66666666</td>                    <td>777777</td>                    <td>88888</td>                    <td>9999</td>                    <td>10559</td>                </tr>            </tbody>        </table>    </div></body></html>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
碧血玉叶花
推荐于2018-06-22 · TA获得超过4976个赞
知道大有可为答主
回答量:6154
采纳率:0%
帮助的人:1734万
展开全部
给你提供个思路,在表格里套表格:把除第一行和第一列外的td合并,然后在里面添加一个
<div>
<table>
</table>
</div>

其中table里放什么东西自己定,最后设计css:
给div设定一个宽度和一个高度,并设置overflow:auto;属性

这样就会根据你table里的内容自动出现滚动条,但有一点,滚动条是在div标签出现,而不是你提供的图片那样(在外部表格的"局部"出现(这种估计实现不了))
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2015-07-17
展开全部
分成两个Table 第二个装div里面不就行了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式