在 CSS 中堆叠 table 的整列
Stack entire column for table in CSS
在 CSS 中有没有办法堆叠整列,所以如果我的桌面 table 如下:
| H1 | H2 | H3 | H4 |
| A1 | B1 | C1 | D1 |
| A2 | B2 | C2 | D2 |
移动端会变成这样:
| H1 |
| A1 |
| A2 |
| H2 |
| B1 |
| B2 |
| H3 |
| C1 |
| C2 |
| H4 |
| D1 |
| D2 |
我希望这是有道理的
这是我的 html 结构:
<table class="rds-table">
<tr>
<th>H1</th>
<th>H2</th>
<th>H3</th>
<th>H4</th>
</tr>
<tr>
<td>A1</td>
<td>B1</td>
<td>C1</td>
<td>D1</td>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
<td>C2</td>
<td>D2</td>
</tr>
<tr>
<td>A3</td>
<td>B3</td>
<td>C3</td>
<td>D3</td>
</tr>
</table>
.column {
float:left;
}
.element {
display: block;
float:left;
clear:both;
}
@media (max-width: 980px) {
.column {
clear:both;
}
}
<div class="rds-table">
<div class="column">
<div class="element">Header One</div>
<div class="element">Data One</div>
<div class="element">Data One</div>
</div>
<div class="column">
<div class="element">Header Two</div>
<div class="element">Data Two</div>
<div class="element">Data Two</div>
</div>
</div>
媒体查询将查找 980 或更小的屏幕尺寸。编辑:现在有更新的代码,这将使用 div 而不是 table 元素。
在您的 css 中使用 @media 查询屏幕尺寸并在 flex 布局中组织内容。
$(window).on('resize', function() {
myfunction();
});
$(document).ready(function() {
$(window).trigger('resize');
});
function myfunction() {
if (window.matchMedia('(max-width: 880px)').matches) {
if (!$(".th").length) {
$(".rds-table tbody").children().each(function(index) {
console.log(index + ": " + $(this).prop("tagName"));
if (index > 0) { //si nos brincamos el header y vamos a los demás tr
$(this).prepend('<td class="th">' + $(".rds-table tr th:nth-child(" + index + ")").text() + '</td>');
$(".rds-table tr th:nth-child(" + index + ")").addClass("invisible");
}
});
}
} else {
/* $(".th").addClass("invisible");*/
$(".th").remove();
$(".invisible").removeClass("invisible");
}
}
@media (max-width: 880px) {
td {
display: flex;
}
}
.th {
color: blue;
}
.invisible {
display: none;
}
th,
td {
border: 1px;
border-style: dashed;
border-color: gray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="rds-table">
<tr>
<th>Header One</th>
<th>Header Two</th>
<th>Header Three</th>
<th>Header Four</th>
</tr>
<tr>
<td>Data One H1</td>
<td>Data Two H1</td>
<td>Data Three H1</td>
<td>Data Four H1</td>
</tr>
<tr>
<td>Data One H2</td>
<td>Data Two H2</td>
<td>Data Three H2</td>
<td>Data Four H2</td>
</tr>
<tr>
<td>Data One H3</td>
<td>Data Two H3</td>
<td>Data Three H3</td>
<td>Data Four H3</td>
</tr>
<tr>
<td>Data One H4</td>
<td>Data Two H4</td>
<td>Data Three H4</td>
<td>Data Four H4</td>
</tr>
</table>
当您使用 Jquery 时,您可以欣赏到边框是完美的,但是当您使用 css 时,它可能会产生细微的错误(比较小提琴 My fiddle and Brendan's fiddle)。
在 CSS 中有没有办法堆叠整列,所以如果我的桌面 table 如下:
| H1 | H2 | H3 | H4 |
| A1 | B1 | C1 | D1 |
| A2 | B2 | C2 | D2 |
移动端会变成这样:
| H1 |
| A1 |
| A2 |
| H2 |
| B1 |
| B2 |
| H3 |
| C1 |
| C2 |
| H4 |
| D1 |
| D2 |
我希望这是有道理的
这是我的 html 结构:
<table class="rds-table">
<tr>
<th>H1</th>
<th>H2</th>
<th>H3</th>
<th>H4</th>
</tr>
<tr>
<td>A1</td>
<td>B1</td>
<td>C1</td>
<td>D1</td>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
<td>C2</td>
<td>D2</td>
</tr>
<tr>
<td>A3</td>
<td>B3</td>
<td>C3</td>
<td>D3</td>
</tr>
</table>
.column {
float:left;
}
.element {
display: block;
float:left;
clear:both;
}
@media (max-width: 980px) {
.column {
clear:both;
}
}
<div class="rds-table">
<div class="column">
<div class="element">Header One</div>
<div class="element">Data One</div>
<div class="element">Data One</div>
</div>
<div class="column">
<div class="element">Header Two</div>
<div class="element">Data Two</div>
<div class="element">Data Two</div>
</div>
</div>
媒体查询将查找 980 或更小的屏幕尺寸。编辑:现在有更新的代码,这将使用 div 而不是 table 元素。
在您的 css 中使用 @media 查询屏幕尺寸并在 flex 布局中组织内容。
$(window).on('resize', function() {
myfunction();
});
$(document).ready(function() {
$(window).trigger('resize');
});
function myfunction() {
if (window.matchMedia('(max-width: 880px)').matches) {
if (!$(".th").length) {
$(".rds-table tbody").children().each(function(index) {
console.log(index + ": " + $(this).prop("tagName"));
if (index > 0) { //si nos brincamos el header y vamos a los demás tr
$(this).prepend('<td class="th">' + $(".rds-table tr th:nth-child(" + index + ")").text() + '</td>');
$(".rds-table tr th:nth-child(" + index + ")").addClass("invisible");
}
});
}
} else {
/* $(".th").addClass("invisible");*/
$(".th").remove();
$(".invisible").removeClass("invisible");
}
}
@media (max-width: 880px) {
td {
display: flex;
}
}
.th {
color: blue;
}
.invisible {
display: none;
}
th,
td {
border: 1px;
border-style: dashed;
border-color: gray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="rds-table">
<tr>
<th>Header One</th>
<th>Header Two</th>
<th>Header Three</th>
<th>Header Four</th>
</tr>
<tr>
<td>Data One H1</td>
<td>Data Two H1</td>
<td>Data Three H1</td>
<td>Data Four H1</td>
</tr>
<tr>
<td>Data One H2</td>
<td>Data Two H2</td>
<td>Data Three H2</td>
<td>Data Four H2</td>
</tr>
<tr>
<td>Data One H3</td>
<td>Data Two H3</td>
<td>Data Three H3</td>
<td>Data Four H3</td>
</tr>
<tr>
<td>Data One H4</td>
<td>Data Two H4</td>
<td>Data Three H4</td>
<td>Data Four H4</td>
</tr>
</table>
当您使用 Jquery 时,您可以欣赏到边框是完美的,但是当您使用 css 时,它可能会产生细微的错误(比较小提琴 My fiddle and Brendan's fiddle)。