gpt4 book ai didi

javascript - 保持表头可见而不在表体中显示滚动条

转载 作者:太空宇宙 更新时间:2023-11-04 01:16:05 25 4
gpt4 key购买 nike

我正在制作一个带有表格数据的页面,该页面可以使用主页滚动条滚动,但保持表头可见。页面顶部有页眉,页面底部有分页区域。

我在这里试了一下。

var dataHeader = document.querySelector('.data thead');

var scrollValue = 50;

window.onscroll = function () {
if (document.body.scrollTop > scrollValue || document.documentElement.scrollTop > scrollValue) {
dataHeader.className = "fixed";
} else {
dataHeader.className = "";
}
};
.main {
position: relative;
top: 0;
width: 700px;
margin: 0 auto;
}
.header {
position: fixed;
top: 0;
background-color: #abc;
width: 700px;
z-index: 10;
}

.content {
position: relative;
top: 60px;
background: #cfe;
}

.content-header {
color: blue;
padding-top: 10px;
}

.data {
position: relative;
margin-bottom: 18px;
}

.data table {
border-collapse: collapse;
width: 700px;
}

.data thead {
background: grey;
width: 700px;
display: table-header-group;
}

.data tbody {
width: 700px;
}

.data thead.fixed {
position: fixed;
top: 80px;
}

.data thead.fixed th {
width: 90px;
}

.paging {
position: absolute;
bottom: 0;
background-color: black;
color: white;
width: 700px;
}

.column {
padding: 2;
width: 80px;
}
<div class="main">

<div class="header">
<h1>Super Page</h1>
</div>

<div class="content">

<div class="content-header">
<h2>Beautiful Content</h2>
</div>

<div class="data">
<table>
<colgroup>
<col class="column">
<col class="column">
<col class="column">
</colgroup>
<thead>
<tr>
<th class="column">Sr</th>
<th class="column">City</th>
<th class="column">Country</th>
</tr>
<thead>
<tbody>
<tr>
<td>1</td>
<td>Amsterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>2</td>
<td>Lahore</td>
<td>Pakistan</td>
</tr>
<tr>
<td>3</td>
<td>Doha</td>
<td>Qatar</td>
</tr>
<tr>
<td>4</td>
<td>Mumbai</td>
<td>India</td>
</tr>
<tr>
<td>5</td>
<td>Rotterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>6</td>
<td>Amsterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>7</td>
<td>Lahore</td>
<td>Pakistan</td>
</tr>
<tr>
<td>8</td>
<td>Doha</td>
<td>Qatar</td>
</tr>
<tr>
<td>9</td>
<td>Mumbai</td>
<td>India</td>
</tr>
<tr>
<td>10</td>
<td>Rotterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>11</td>
<td>Amsterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>12</td>
<td>Lahore</td>
<td>Pakistan</td>
</tr>
<tr>
<td>13</td>
<td>Doha</td>
<td>Qatar</td>
</tr>
<tr>
<td>14</td>
<td>Mumbai</td>
<td>India</td>
</tr>
<tr>
<td>15</td>
<td>Rotterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>16</td>
<td>Amsterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>17</td>
<td>Lahore</td>
<td>Pakistan</td>
</tr>
<tr>
<td>18</td>
<td>Doha</td>
<td>Qatar</td>
</tr>
<tr>
<td>19</td>
<td>Mumbai</td>
<td>India</td>
</tr>
<tr>
<td>20</td>
<td>Rotterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>21</td>
<td>Prague</td>
<td>Czech Republic</td>
</tr>
</tbody>
</table>
</div>
<!-- /.data -->

<div class="paging">
Displaying 10 of 100 records
</div>
</div>
<!-- /.content -->

</div>
<!-- /.main -->

唯一的问题是当我向下滚动时,表格标题会折叠并且不会与表格主体的列保持对齐。我该如何解决?

最佳答案

你可以调整你的 .data thead.fixed th 规则为 width: calc(700px/3); 像这样:

var dataHeader = document.querySelector('.data thead');

var scrollValue = 50;

window.onscroll = function () {
if (document.body.scrollTop > scrollValue || document.documentElement.scrollTop > scrollValue) {
dataHeader.className = "fixed";
} else {
dataHeader.className = "";
}
};
.main {
position: relative;
top: 0;
width: 700px;
margin: 0 auto;
}
.header {
position: fixed;
top: 0;
background-color: #abc;
width: 700px;
z-index: 10;
}

.content {
position: relative;
top: 60px;
background: #cfe;
}

.content-header {
color: blue;
padding-top: 10px;
}

.data {
position: relative;
margin-bottom: 18px;
}

.data table {
border-collapse: collapse;
width: 700px;
}

.data thead {
background: grey;
width: 700px;
display: table-header-group;
}

.data tbody {
width: 700px;
}

.data thead.fixed {
position: fixed;
top: 80px;
}

.data thead.fixed th {
width: calc(700px/3);
}

.paging {
position: absolute;
bottom: 0;
background-color: black;
color: white;
width: 700px;
}

.column {
padding: 2;
width: 80px;
<div class="main">

<div class="header">
<h1>Super Page</h1>
</div>

<div class="content">

<div class="content-header">
<h2>Beautiful Content</h2>
</div>

<div class="data">
<table>
<colgroup>
<col class="column">
<col class="column">
<col class="column">
</colgroup>
<thead>
<tr>
<th class="column">Sr</th>
<th class="column">City</th>
<th class="column">Country</th>
</tr>
<thead>
<tbody>
<tr>
<td>1</td>
<td>Amsterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>2</td>
<td>Lahore</td>
<td>Pakistan</td>
</tr>
<tr>
<td>3</td>
<td>Doha</td>
<td>Qatar</td>
</tr>
<tr>
<td>4</td>
<td>Mumbai</td>
<td>India</td>
</tr>
<tr>
<td>5</td>
<td>Rotterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>6</td>
<td>Amsterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>7</td>
<td>Lahore</td>
<td>Pakistan</td>
</tr>
<tr>
<td>8</td>
<td>Doha</td>
<td>Qatar</td>
</tr>
<tr>
<td>9</td>
<td>Mumbai</td>
<td>India</td>
</tr>
<tr>
<td>10</td>
<td>Rotterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>11</td>
<td>Amsterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>12</td>
<td>Lahore</td>
<td>Pakistan</td>
</tr>
<tr>
<td>13</td>
<td>Doha</td>
<td>Qatar</td>
</tr>
<tr>
<td>14</td>
<td>Mumbai</td>
<td>India</td>
</tr>
<tr>
<td>15</td>
<td>Rotterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>16</td>
<td>Amsterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>17</td>
<td>Lahore</td>
<td>Pakistan</td>
</tr>
<tr>
<td>18</td>
<td>Doha</td>
<td>Qatar</td>
</tr>
<tr>
<td>19</td>
<td>Mumbai</td>
<td>India</td>
</tr>
<tr>
<td>20</td>
<td>Rotterdam</td>
<td>Netherlands</td>
</tr>
<tr>
<td>21</td>
<td>Prague</td>
<td>Czech Republic</td>
</tr>
</tbody>
</table>
</div>
<!-- /.data -->

<div class="paging">
Displaying 10 of 100 records
</div>
</div>
<!-- /.content -->

</div>
<!-- /.main -->

关于javascript - 保持表头可见而不在表体中显示滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50114978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com