前段时间在网上看到了一个老外写的一个HTML5响应式表格效果,它的CSS代码用SASS写的,有许多重复的data
属性。我们这里改进一下他的代码,解决一下他写的不好的地方。要看到本例的响应式表格效果,浏览器要缩放到小于600像素的大小。HTML结构如下:
<table id="miyazaki"> <caption>The Films of Miyazaki</caption> <thead> <tr><th>Film<th>Year<th>Honor <tbody> <tr> <td data-th="Film">My Neighbor Totoro <td data-th="Year">1988 <td data-th="Honor">Blue Ribbon Award (Special) <tr> <td data-th="Film">Princess Mononoke <td data-th="Year">1997 <td data-th="Honor">Nebula Award (Best Script) <tr> <td data-th="Film">Spirited Away <td data-th="Year">2001 <td data-th="Honor">Academy Award (Best Animated Feature) <tr> <td data-th="Film">Howl’s Moving Castle <td data-th="Year">2004 <td data-th="Honor">Hollywood Film Festival (Animation OTY) </table>
注意代码中的data
属性,每一个单元格的data
属性都与表格的header相对应。
CSS样式
表格基本的CSS样式如下:
table#miyazaki caption { font-size: 2rem; color: #444; margin: 1rem; background-image: url(miyazaki.png), url(miyazaki2.png); background-size: contain; background-repeat: no-repeat; background-position: center left, center right; } table#miyazaki { border-collapse: collapse; font-family: Agenda-Light; font-weight: 100; background: #333; color: #fff; text-rendering: optimizeLegibility; border-radius: 5px; } table#miyazaki thead th { font-weight: 600; } table#miyazaki thead th, table#miyazaki tbody td { padding: .8rem; font-size: 1.4rem; } table#miyazaki tbody td { padding: .8rem; font-size: 1.4rem; color: #444; background: #eee; } table#miyazaki tbody tr:not(:last-child) { border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; }
下面是响应式表格的CSS代码:
@media screen and (max-width: 600px) { table#miyazaki caption { background-image: none; } table#miyazaki thead { display: none; } table#miyazaki tbody td { display: block; padding: .6rem; } table#miyazaki tbody tr td:first-child { background: #333; color: #fff; } table#miyazaki tbody td:before { content: attr(data-th); font-weight: bold; display: inline-block; width: 6rem; } }
media query代码中隐藏表格的头部单元,并且将每一个单元格的data-th
作为标签显示在单元格内容的前面。每一行的第一个单元格都设置了特别的背景色和前景色,使之更为清晰。
扩展
你现在可以缩放浏览器来看看效果,非常不错。但是上面的代码是不可扩展的:要添加一个新行必须手动为每个单元格添加一个data-th
属性。要想做到自动化,可以在服务器端实现,如PHP。也可以通过javascript来实现它。
首先,将整个表格都进行简化:
<table id="miyazaki"> <caption>The Films of Hayao Miyazaki</caption> <thead> <tr><th>Film<th>Year<th>Honor <tbody> <tr> <td>My Neighbor Totoro <td>1988 <td>Blue Ribbon Award (Special) <tr> <td>Princess Mononoke <td>1997 <td>Nebula Award (Best Script) <tr> <td>Spirited Away <td>2001 <td>Academy Award (Best Animated Feature) <tr> <td>Howl’s Moving Castle <td>2004 <td>Hollywood Film Festival (Animation OTY) </table>
然后在文档的底部添加下面的javascript代码:
<script> var headertext = []; var headers = document.querySelectorAll("#miyazaki th"), tablerows = document.querySelectorAll("#miyazaki th"), tablebody = document.querySelector("#miyazaki tbody"); for(var i = 0; i < headers.length; i++) { var current = headers[i]; headertext.push( current.textContent.replace( /\r?\n|\r/,"") ); } for (var i = 0, row; row = tablebody.rows[i]; i++) { for (var j = 0, col; col = row.cells[j]; j++) { col.setAttribute("data-th", headertext[j]); } } </script>
上面的代码的意思是:获取每一个<th>
中的文本内容,然后分别剔除它们的回车和换行符。然后将这些文本分别添加到适当的单元格的data
属性上,添加的规则与CSS样式的规则相一致。(使用setAttribute
要比dataset
要好,后者只有在IE 11中得到支持。)
到此,这个HTML5响应式表格就完成了。