【发布时间】:2016-07-05 10:49:48
【问题描述】:
我正在尝试让表格在按钮单击时向左/向右滚动,以将第一列保持在原位。 我的数据都在一个表中,所以我不能创建两个表而不是一个表。有没有办法做到这一点?请参阅下面的小提琴。
jQuery:
$(document).ready(function() {
$("#right").on("click", function() {
var position = $("table").position();
$("table").animate({
left: position.left - 200
}, 800);
});
});
$("#left").on("click", function() {
var position = $("table").position();
$("table").animate({
left: position.left + 200
}, 800);
});
CSS:
#table-wrapper {
width: 95%;
float: left;
overflow-x: scroll;
background: #ddd;
}
table {
background: #fff;
width: 1200px;
text-align:center;
overflow: hidden;
position:relative;
}
table thead tr th:first-child,
table tbody tr td:first-child {
background: yellow;
top: auto;
left: 0.5;
position: absolute;
width: 6em;
}
HTML:
<button id="left">←</button>
<button id="right">→</button>
<div id="table-wrapper">
<table border="1">
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
<th>Heading5</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
<tr>
<td>2</td>
<td>22</td>
<td>23</td>
<td>24</td>
<td>25</td>
</tr>
<tr>
<td>3</td>
<td>32</td>
<td>33</td>
<td>34</td>
<td>35</td>
</tr>
</tbody>
</table>
</div>
这是我的fiddle。
【问题讨论】:
-
考虑纯 CSS 解决方案:stackoverflow.com/q/15811653/1066234