页面跳转指定位置
官方文档
https://developer.mozilla.org/zhCN/docs/Web/API/Element/scrollIntoView
1,给页面DOM动态添加 ID
2,然后利用scrollIntoView 跳转指定位置
const elementId = arr.substring(14) // 刷新或者从其他路由跳来的ID
const element: HTMLElement | null = document.getElementById(elementId) // TS语法比较严格需要加HTMLElement | null
element?.scrollIntoView()

这篇博客介绍了如何利用JavaScript的Element.scrollIntoView方法动态为DOM元素添加ID,并在页面加载或路由跳转后,将页面滚动到指定元素的位置。通过示例代码展示了TS语法中如何安全调用该方法。
3949

被折叠的 条评论
为什么被折叠?



