CSS粘性,桌侧栏第一列不粘

发布于 2025-01-25 09:07:18 字数 1471 浏览 2 评论 0 原文

对于一个项目,我需要桌子头和侧边栏(前6列)是固定/粘性的。

到目前为止,我的解决方案可以使用,但对浏览器(只有Chrome和Edge)都没有。 在Firefox和Safari中,我有一个问题,即第一列在滑到侧面和向下时不会保持固定。第2列和以下内容保持固定。

.sidebar{
position: sticky;
}

.column1{
left:0;
}

.column2{
left: 10vw;
}

.column3{
left: 20vw;
}

.column4{
left: 30vw;
}

.column5{
left: 40vw;
}

.column6{
left: 50vw;
}
<table>
    <thead>
        <tr>
            <td class="th1">1</td>
            <td class="th2">2</td>
            <td class="th3">3</td>
            <td class="th4">4</td>
            <td class="th5">5</td>
            <td class="th6">6</td>
            
            <td> a lot more tds </td>
        </tr>
    </thead>
    
    <tbody>
        <tr>
            <td class="sidebar column1">col 1</td>
            <td class="sidebar column2">col 2</td>
            <td class="sidebar column3">col 3</td>
            <td class="sidebar column4">col 4</td>
            <td class="sidebar column5">col 5</td>
            <td class="sidebar column6">col 6</td>
            
            <td> a lot more tds </td>
        </tr>
    </tbody>
</table>

For a project I need the table head and sidebar (first 6 columns) to be fixed/sticky.

My solution so far works, but not for ever browser (only chrome and edge).
In firefox and safari I have the issue that the first column doesnt stay fixed when sliding to the side and down. Column2 and following stay fixed as they are supposed to.

.sidebar{
position: sticky;
}

.column1{
left:0;
}

.column2{
left: 10vw;
}

.column3{
left: 20vw;
}

.column4{
left: 30vw;
}

.column5{
left: 40vw;
}

.column6{
left: 50vw;
}
<table>
    <thead>
        <tr>
            <td class="th1">1</td>
            <td class="th2">2</td>
            <td class="th3">3</td>
            <td class="th4">4</td>
            <td class="th5">5</td>
            <td class="th6">6</td>
            
            <td> a lot more tds </td>
        </tr>
    </thead>
    
    <tbody>
        <tr>
            <td class="sidebar column1">col 1</td>
            <td class="sidebar column2">col 2</td>
            <td class="sidebar column3">col 3</td>
            <td class="sidebar column4">col 4</td>
            <td class="sidebar column5">col 5</td>
            <td class="sidebar column6">col 6</td>
            
            <td> a lot more tds </td>
        </tr>
    </tbody>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美的太阳 2025-02-01 09:07:18

位置:粘性; 在大多数现代浏览器上工作正常,您可以在此处看到

https://caniuse.com/?search=position%20%3asticky%20

但是,粘性元素的行为就像相对的作用,

直到给定位置
例如, top:0;

这是粘性的行为与元素边界达到指定位置(顶部:0)时相对的行为不同

,因此它开始像固定元素一样起作用,

所以我认为您的问题是省略了TOP或左值但是您的代码在这里不完整,您正在设置位置:粘性; to .sidebar ,在您的html中未使用...-

= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -

您的表格和TH元素也应具有 width 与您的TD元素一起,

我尝试了它使用 width ,如您所述,它可以使用

“ https://jsfiddle.net/mahdiar_mansouri/27k9cv6f/3/”

position:sticky; works fine on most modern browsers as you can see here

https://caniuse.com/?search=Position%20%3Asticky%20

BUT, sticky elements act JUST LIKE relative ones

UNTIll a given position
for example top:0;

This is when sticky behaves different than relative

When element's boundary reaches the specified position (top:0) it start to act LIKE a fixed element

So i Think your problem is omitting top or left values however your code is not complete here, you are setting position:sticky ; to .sidebar which is not used in your HTML...

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Your table and th elements should also have width alongside your td elements,

I tried it using width and it worked as you described

here's the fiddle link to it

https://jsfiddle.net/mahdiar_mansouri/27k9cv6f/3/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文