jquery - 滚动到固定高度表中的事件表行

标签 jquery css

我在一个 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/

相关文章:

javascript - 如果元素具有类和特定值,则 jquery 显示内容

javascript - 禁用 HTML 输入列表的建议

jquery - Microsoft JScript 运行时错误 : Unable to get value of the property 'defaultView' : object is null or undefined

javascript - 如何将javascript添加到按钮?

html - 我必须将页眉的宽度设置为大于主体才能完全覆盖。为什么?

jQuery - KeyPress/KeyCode 与进入点击按钮功能

javascript - 如何隐藏包含特定值的类?

python - Flask wtforms 的 CSS

javascript - 带有列表项计算javascript的待办事项列表

javascript - 通过 Javascript 设置段落 CSS 宽度不起作用