使用javascript将网页滚动到指定位置



在一个javascript web应用程序中,我显示了一个相当长的表,其中充满了来自firebase的数据,代码如下。

̧  const dbReference = firebase.database().ref('MyCollection').child('CHD')
dbReference.on('value', function(list) {
list.forEach(function(item) {
itemR = document.createElement('tr'),
itemD = document.createElement('td'),
.... useful code laying out the item inside the table ....
}
}

它按预期工作,最后我在一个表中列出了我的项目列表。但我需要更多的东西。

如何使窗口自动滚动到指定位置。例如,将列表中的第157个项目显示在窗口顶部。我已经在网上搜索过了,发现了一些我尝试过的滚动功能,但没有成功。

您可以向所有项目添加一个id属性,然后将一个锚标记指向它

<a href="#157">SCROLL</a>
...
<td id="157"></td>

最新更新