我在一个 div 中有一个固定高度的表格。我想滚动到该表中的特定行。我提到了这个链接 http://jsfiddle.net/SZKJh/1/ ,但它不适用于固定的桌面高度。 下面是代码片段,请帮助我解决这个问题。
function GotoRow(){
var w = $('#forPmpIterate'); // my div
var row = $('#tableiterate').find('tr')
.removeClass('active')
.eq( +$('#line').val() )
.addClass('active');
if (row.length){
$('#forPmpIterate').animate({scrollTop: row.offset().top - (w.height()/2)});
}
}
<div id="forPmpIterate">
<div id="control" style="float:right;padding-right:15px">
Go to stage: <input type="text" size="5" id="line" />
<input type="button" onclick="GotoRow()" value="Go" />
</div>
<table id="tableiterate" class="table table-fixed">
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr>
<!-- many rows here -->
</tr>
</tbody>
我的风格如下,
<style>
/* */
.table-fixed thead {
width: 100%; /*97%;*/
padding-left: 4px;
}
.table-fixed tbody {
height: 230px;
overflow-y: auto;
width: 100%;
}
.table-fixed thead, .table-fixed tbody, .table-fixed tr, .table-fixed td, .table-fixed th {
display: block;
}
.table-fixed tbody td {
float: left;
border-bottom-width: 0;
border: 1px solid #eee;
}
.table-fixed thead > tr > th {
float: left;
border-bottom-width: 0;
background-color: #eee;
padding-left: 4px;
}
</style>
最佳答案
我已经解决了你的问题,这是更新后的代码 http://jsfiddle.net/SZKJh/1084/
这是代码
scrollTop: row.offset().top - singleTr.clientHeight
所以基本上你只需要用 tr 的高度减去事件行的顶部位置值。这就是您获得正确滚动位置的方式。
关于jquery - 滚动到固定高度表中的事件表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41011426/