简体   繁体   English

如何自动滚动到水平表格中的特定表格单元格

[英]How to auto scroll to a specific table cell in a horizontal table

In my current project I have a table contained within a div that has 10 cells positioned horizontally. 在我当前的项目中,我有一个包含在div中的表格,该表格中有10个水平放置的单元格。 This is done using the following code below... 这是使用下面的代码完成的...

<div id="dashboard">
    <table>
        <tr>
            <td><div id="cell1"></div></td>
            <td><div id="cell2"></div></td>
            <td><div id="cell3"></div></td>
            <td><div id="cell4"></div></td>
            <td><div id="cell5"></div></td>
            <td><div id="cell6"></div></td>
            <td><div id="cell7"></div></td>
            <td><div id="cell8"></div></td>
            <td><div id="cell9"></div></td>
            <td><div id="cell10"></div></td>
        </tr>
    </table>
</div>

I already have code saying that if a user clicks on a button that corresponds to a cell in the table, the cell will highlight yellow. 我已经有代码说,如果用户单击与表中单元格相对应的按钮,则该单元格将突出显示黄色。 Since the table's width is larger than the horizontal width of the page's container, I had to set the div table overflow property to auto. 由于表的宽度大于页面容器的水平宽度,因此我不得不将div表的溢出属性设置为auto。 How can I make it so that if the user clicks a button for a specific cell that is not in view or only has some part of the cell in view , the table will auto scroll horizontally (left or right depending on the cell's location and the current view) to that specific cell. 我该如何做到这一点,以便如果用户单击某个不在视图中的特定单元格的按钮, 或者只单击视图中某个单元格的一部分 ,则表格将自动水平滚动 (根据单元格的位置和位置自动向左或向右滚动)当前视图)。

Is there a JavaScript solution that would allow me to do this? 是否有JavaScript解决方案可以让我做到这一点?

In order to scroll at any position within the same page, you can use the following: 为了在同一页面内的任何位置滚动,可以使用以下命令:

Mark the element, where you want to scroll to, with this tag: 使用以下标记标记要滚动到的元素:

<a id="cellcontent">

And in order to scroll to that marker, just use a simple a -tag: 为了滚动到该标记,只需使用一个简单a -tag即可:

<a href="#cellcontent">go to cell</a>

Alternatevely, you can try it with JavaScript: 另外,您可以尝试使用JavaScript:

<script type="text/javascript">    
    function showIt(elID) {
        var el = document.getElementById(elID);
        el.scrollIntoView(true);
    }    
</script>

Source: https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView 来源: https : //developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView

Edit: As Pluto suggested in the comments, the better approach is to put the id-tag directly into the cell: 编辑:正如冥王星在评论中建议的那样,更好的方法是将id标签直接放入单元格中:

<td id="cellcontent">Some content</td>

Apart from @maja's solution, you can also do it with javascript using the cell's left position with the function getBoundingClientRect() minus its margin and padding. 除了@maja的解决方案之外,您还可以使用JavaScript使用单元格的左侧位置使用函数getBoundingClientRect()减去边距和填充来完成此任务。 Then moving the window or scroll container to that scroll position with element.scrollTo() function Ex: 然后使用element.scrollTo()函数Ex将窗口或滚动容器移动到该滚动位置:

window.scrollTo(document.getElementById("cell4").getBoundingClientRect().left - 30,0);

Check this fiddle: http://jsfiddle.net/RXKmY/ 检查此小提琴: http : //jsfiddle.net/RXKmY/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM