简体   繁体   English

带有水平滚动的表格,左列有粘性,屏幕顶部有粘性 header

[英]Table with horizontal scroll with sticky left column and sticky header on top of screen

I want to create a table with a horizontal scroll and a sticky left column and a sticky header on top of the screen.我想在屏幕顶部创建一个带有水平滚动条和粘性左列以及粘性 header 的表格。 The table is wide, so I want to scroll it horizontally, but I need to make the first left column sticky.表格很宽,所以我想水平滚动它,但我需要将第一个左边的列设为粘性。 I also need to make the first row (header) sticky on top of the screen when the page is scrolled down.当页面向下滚动时,我还需要使第一行(标题)粘在屏幕顶部。 I don't know if these two options are compatible together.我不知道这两个选项是否兼容。 I've tried almost everything.我几乎尝试了一切。 Here is my latest code, which almost works, but the first row is not scrolling with the rest of the table.这是我的最新代码,它几乎可以工作,但是第一行没有随着表格的 rest 滚动。 It could be made of divs or table elements, it doesn't matter.它可以由 div 或表格元素组成,这并不重要。

I want to avoid using JavaScript.我想避免使用 JavaScript。

 body { background: #F5F7FA; overflow-x: hidden; }.wrap { position: relative; margin: 10em auto 30em; max-width: 960px; overscroll-behavior: contain; }.headers { top: 0; position: -webkit-sticky; position: sticky; z-index: 1; }.tracks, .scroller { display: flex; overflow-y: hidden; -webkit-overflow-scrolling: touch; }.scroller { overflow-x: hidden; }.tracks { overflow: auto; scroll-snap-type: x mandatory; }.scenes::-webkit-scrollbar, .scroller::-webkit-scrollbar { display: none; }.track { flex: 1 0 calc(22% + 2px); scroll-snap-align: start; }.track.first { position: sticky; left: 0; }.sticky { position: sticky; top: 0; }.track +.track { margin-left: -1px; }.heading { height: 50px; display: flex; justify-content: center; align-items: center; position: -webkit-sticky; position: sticky; top: 0; border: solid #fff; border-width: 0 1px; z-index: 1; background: #efefef; font-weight: 700; }.entry { border: 1px solid #ebebeb; border-top: 0; background: #fff; height: 8em; padding: 1em; } @media (max-width: 767px) {.track { flex: 1 0 calc(50% + 7px); } }
 <div class="wrap"> <div class="headers"> <div class="scroller"> <div class="track"> <div class="heading">Heading 1</div> </div> <div class="track"> <div class="heading">Heading 2</div> </div> <div class="track"> <div class="heading">Heading 3</div> </div> <div class="track"> <div class="heading">Heading 4</div> </div> <div class="track"> <div class="heading">Heading 5</div> </div> </div> </div> <div class="tracks"> <div class="track first"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> <div class="track"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> <div class="track"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> <div class="track"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> <div class="track"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> </div> </div>

在此处输入图像描述

Option 1: Header scrolls with the rest of the table.方案一:Header与表格的rest滚动。 Option 2: Completely different solution with the same result.选项 2:具有相同结果的完全不同的解决方案。

This is what it should look like, but without using JavaScript only with pure CSS.这是它应该看起来的样子,但没有使用 JavaScript 仅使用纯 CSS。

 function scrollSynchronization() { const wrapper1 = document.querySelector('.scroller'); const wrapper2 = document.querySelector('.tracks'); wrapper1.addEventListener('scroll', function() { wrapper2.scrollLeft = wrapper1.scrollLeft; }); wrapper2.addEventListener('scroll', function() { wrapper1.scrollLeft = wrapper2.scrollLeft; }); } scrollSynchronization();
 body { background: #F5F7FA; overflow-x: hidden; }.wrap { position: relative; margin: 10em auto 30em; max-width: 960px; }.headers { top: 0; position: -webkit-sticky; position: sticky; z-index: 1; }.tracks, .scroller { display: flex; overflow-y: hidden; }.scroller { overflow-x: scroll; }.tracks { overflow: auto; }.scenes::-webkit-scrollbar, .scroller::-webkit-scrollbar { display: none; }.track { flex: 1 0 calc(22% + 2px); }.track.first { position: sticky; left: 0; }.sticky { position: sticky; top: 0; }.track +.track { margin-left: -1px; }.heading { height: 50px; display: flex; justify-content: center; align-items: center; position: -webkit-sticky; position: sticky; top: 0; border: solid #fff; border-width: 0 1px; z-index: 1; background: #efefef; font-weight: 700; }.entry { border: 1px solid #ebebeb; border-top: 0; background: #fff; height: 8em; padding: 1em; } @media (max-width: 767px) {.track { flex: 1 0 calc(50% + 7px); } }
 <div class="wrap"> <div class="headers"> <div class="scroller"> <div class="track"> <div class="heading">Heading 1</div> </div> <div class="track"> <div class="heading">Heading 2</div> </div> <div class="track"> <div class="heading">Heading 3</div> </div> <div class="track"> <div class="heading">Heading 4</div> </div> <div class="track"> <div class="heading">Heading 5</div> </div> </div> </div> <div class="tracks"> <div class="track first"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> <div class="track"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> <div class="track"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> <div class="track"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> <div class="track"> <div class="entry"> <h3>Lorem, ipsum dolor.</h3> </div> <div class="entry"> <h3>Lorem, ipsum.</h3> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM