gpt4 book ai didi

javascript - 使用 colspan 在 上定位 Sticky?

转载 作者:行者123 更新时间:2023-12-05 00:29:03 26 4
gpt4 key购买 nike

好像是 sticky不适用于 <td>colspan .有办法解决吗?

body {
font-family: 'Lucida Grande';
}

div {
width: 500px;
height: 200px;
overflow: scroll;
}

td,
th {
padding: 2px 5px;
white-space: nowrap;
}

.sticky-header {
position: sticky;
position: -webkit-sticky;
top: 0;
background: #146775;
color: white;
z-index: 3;
}

.sticky-header:first-child {
left: 0;
z-index: 4;
}

.sticky-row {
position: sticky;
position: -webkit-sticky;
left: 0;
top: 23px; /* This seems to work differently for Safari, FF, Chrome etc. Here adjusted for FF */
background: #1C4464;
color: white;
z-index: 3;
}
.sticky-row:nth-child(2) {
left: 100px; /* How can I make this dynamic? */
z-index: 2;
}
span {
position: absolute;
top: 2px;
left: 113px;
}

.sticky-column {
position: sticky;
position: -webkit-sticky;
left: 0;
background: #569CA8;
color: white;
z-index: 1;
width: 100px; /* How can I make this dynamic? */
}
<div>
<table cellspacing="0">
<thead>
<tr>
<td class="sticky-header">Header 1</td>
<td class="sticky-header">Header 2</td>
<td class="sticky-header">Header 3</td>
<td class="sticky-header">Header 4</td>
<td class="sticky-header">Header 5</td>
<td class="sticky-header">Header 7</td>
<td class="sticky-header">Header 8</td>
<td class="sticky-header">Header 9</td>
<td class="sticky-header">Header 10</td>
</tr>
</thead>
<tbody>
<tr>
<td class="sticky-row">Sticky Row 1</td>
<td class="sticky-row" colspan="9">How can I make this sticky?</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-row">Sticky Row 2<span>Dirty way of making sticky</span></td>
<td class="sticky-row" colspan="9"></td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-row">Sticky Row 3</td>
<td class="sticky-row" colspan="9">How can I make this sticky?</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
</tbody>
</table>
</div>

最佳答案

实际上它已经很粘了,问题是由于 td 的宽度,它没有理由移动。因此,为了使它们都具有粘性,请更改 科尔斯潘在第二个 td 上(为了让它更好地给它一个 50px 的左边,让它们彼此相邻移动)。希望这可以解决您的问题。

更新 :

2nd td 上的 colspan 是您的代码不起作用的原因,我通过将结构替换为此来解决此问题

<tr class="darkblue">
<td class="sticky-row">Sticky Row 1</td>
<td class="sticky-row" colspan>How can I make this sticky?</td>
<td colspan="8"></td>
</tr>

并添加了一个额外的 css 类来修复 tr 的背景颜色
.darkblue {
background: #1C4464;
width: 100%;
}

.sticky-row {
position: sticky;
position: -webkit-sticky;
left: 0;
top: 23px;
color: white;
z-index: 3;
}

下面的完整代码供引用

    body {
font-family: 'Lucida Grande';
}

div {
width: 500px;
height: 200px;
overflow: scroll;
}

td,
th {
padding: 2px 5px;
white-space: nowrap;
}

.sticky-header {
position: sticky;
position: -webkit-sticky;
top: 0;
background: #146775;
color: white;
z-index: 3;
}

.sticky-header:first-child {
left: 0;
z-index: 4;
}

.darkblue {
background: #1C4464;
width: 100%;
}

.sticky-row {
position: sticky;
position: -webkit-sticky;
left: 0;
top: 23px;
/* background: #1C4464; */
color: white;
z-index: 3;
}
.sticky-row:nth-child(2) {
left: 100px; /* How can I make this dynamic? */
z-index: 2;
}
span {
position: absolute;
top: 2px;
left: 113px;
}

.sticky-column {
position: sticky;
position: -webkit-sticky;
left: 0;
background: #569CA8;
color: white;
z-index: 1;
width: 100px; /* How can I make this dynamic? */
}
<div>
<table cellspacing="0">
<thead>
<tr>
<td class="sticky-header">Header 1</td>
<td class="sticky-header">Header 2</td>
<td class="sticky-header">Header 3</td>
<td class="sticky-header">Header 4</td>
<td class="sticky-header">Header 5</td>
<td class="sticky-header">Header 7</td>
<td class="sticky-header">Header 8</td>
<td class="sticky-header">Header 9</td>
<td class="sticky-header">Header 10</td>
</tr>
</thead>
<tbody>
<tr class="darkblue">
<td class="sticky-row">Sticky Row 1</td>
<td class="sticky-row" colspan>How can I make this sticky?</td>
<td colspan="8"></td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr class="darkblue">
<td class="sticky-row">Sticky Row 2<span>Dirty way of making sticky</span></td>
<td class="sticky-row" colspan="9"></td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-row">Sticky Row 3</td>
<td class="sticky-row" colspan="9">How can I make this sticky?</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
<tr>
<td class="sticky-column">Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
<td>Column 7</td>
<td>Column 8</td>
<td>Column 9</td>
<td>Column 10</td>
</tr>
</tbody>
</table>
</div>

关于javascript - 使用 colspan 在 <td> 上定位 Sticky?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54212865/

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