固定表格表头:使用CSS实现class='tt'列固定
<template>
<div class='brand-partyHistory'>
<table>
<thead class='tt'>
<tr>
<th class='base-th' style='width: 167px'>支部名称</th>
<th class='base-th' style='width: 200px'>类型</th>
<th class='base-th' style='width: 116px'>一月</th>
<th class='base-th' style='width: 116px'>二月</th>
<th class='base-th' style='width: 116px'>三月</th>
<th class='base-th' style='width: 116px'>四月</th>
<th class='base-th' style='width: 116px'>五月</th>
<th class='base-th' style='width: 116px'>六月</th>
<th class='base-th' style='width: 116px'>七月</th>
<th class='base-th' style='width: 116px'>8月</th>
<th class='base-th' style='width: 116px'>9月</th>
<th class='base-th' style='width: 116px'>10月</th>
<th class='base-th' style='width: 116px'>11月</th>
<th class='base-th' style='width: 116px'>12月</th>
</tr>
</thead>
<tbody>
<!-- 表格内容 -->
</tbody>
</table>
</div>
</template>
<script>
export default {}
</script>
<style lang='scss' scoped>
.brand-partyHistory {
position: relative;
width: 100%;
height: 100%;
overflow-y: auto;
table {
border-collapse: collapse;
width: 100%;
thead {
tr {
width: 100%;
.base-th {
height: 50px;
color: #333333;
font-size: 14px;
font-weight: 700;
border: 1px solid #dfd6d6;
background-color: #fffbfb;
}
}
}
tbody {
overflow-y: auto;
tr {
width: 100%;
td {
height: 50px;
text-align: center;
font-size: 14px;
font-weight: 400;
color: #333333;
border: 1px solid #dfd6d6;
}
}
}
}
.tt {
position: sticky;
top: 0;
}
}
</style>
<p>可以使用CSS的position属性来实现固定某栏。首先,为class为'tt'的th元素添加一个position: sticky的样式,并设置top属性为0,使其固定在顶部:</p>
<pre><code class="language-css">.tt {
position: sticky;
top: 0;
}
</code></pre>
<p>然后,由于表格的父元素.brand-partyHistory具有overflow-y: auto的样式,需要设置其position为relative,以便让固定的列相对于该父元素进行定位:</p>
<pre><code class="language-css">.brand-partyHistory {
position: relative;
}
</code></pre>
<p>这样,class为'tt'的列就会在滚动时保持固定。完整的代码如下:</p>
<pre><code class="language-html"><template>
<div class='brand-partyHistory'>
<table>
<thead class='tt'>
<tr>
<th class='base-th' style='width: 167px'>支部名称</th>
<th class='base-th' style='width: 200px'>类型</th>
<th class='base-th' style='width: 116px'>一月</th>
<th class='base-th' style='width: 116px'>二月</th>
<th class='base-th' style='width: 116px'>三月</th>
<th class='base-th' style='width: 116px'>四月</th>
<th class='base-th' style='width: 116px'>五月</th>
<th class='base-th' style='width: 116px'>六月</th>
<th class='base-th' style='width: 116px'>七月</th>
<th class='base-th' style='width: 116px'>8月</th>
<th class='base-th' style='width: 116px'>9月</th>
<th class='base-th' style='width: 116px'>10月</th>
<th class='base-th' style='width: 116px'>11月</th>
<th class='base-th' style='width: 116px'>12月</th>
</tr>
</thead>
<tbody>
<!-- 表格内容 -->
</tbody>
</table>
</div>
</template>
<script>
export default {}
</script>
<style lang='scss' scoped>
.brand-partyHistory {
position: relative;
width: 100%;
height: 100%;
overflow-y: auto;
table {
border-collapse: collapse;
width: 100%;
thead {
tr {
width: 100%;
.base-th {
height: 50px;
color: #333333;
font-size: 14px;
font-weight: 700;
border: 1px solid #dfd6d6;
background-color: #fffbfb;
}
}
}
tbody {
overflow-y: auto;
tr {
width: 100%;
td {
height: 50px;
text-align: center;
font-size: 14px;
font-weight: 400;
color: #333333;
border: 1px solid #dfd6d6;
}
}
}
}
.tt {
position: sticky;
top: 0;
}
}
</style>
</code></pre>
原文地址: https://www.cveoy.top/t/topic/ql7C 著作权归作者所有。请勿转载和采集!