Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

<table> : How can <thead> be displayed while scrolling through <tbody> in HTML/CSS

I have a big table with about 300 rows and 12 columns. While scrolling through the table by paging through the page the <thead> content is not displayed by default. I would like to see it while scrolling "within" <tbody>. That is, if the top of the screen starts with a row, I want that the header is displayed first. Otherwise it should behave like a regular table.

The common solution I have seen so far is to create a table that can scroll for itself (thus independently of page scrolling). That is, what answers to this question suggest.

But this is not very practical if you have many columns spanning the screen, in particular because there are now two independent scrollbars. On mobile devices the second scrollbar takes away a lot of precious space. And it is also very irritating to use. You cannot simply "leaf through", but you have to concentrate to hit that tiny scroll bar. On other browsers you can scroll touching the data within, but once you move outside, another irritating movement happens...

Is there a clean CSS way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hoping that position:sticky will one day be the definitive answer to this problem (see announcement here) like mentioned in the comments above, I was still fascinated at making a simple HTML/CSS proof-of-concept, without the double scrollbars.

My simple solution — not meant to be a perfect one, more as an exercise — without scripting:

  • Duplicate the thead, one that scrolls with the page, and one that stays fixed
  • Play with z-indexes to show the appropriate thead

http://jsfiddle.net/willemvb/hEyZh/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...