scrollTop 不生效时如何将页面移动到指定元素位置

{/* 要跳转的暗锚点 */}
<a id="hardware" style={{ position: "relative", top: "0" }}></a>
handleLoaction(item) {
    let ele = document.querySelector(item.location);
    
    // 减去头部导航栏的高度
    ele.style.top = "-100px";
    ele.scrollIntoView({ behavior: "smooth", block: "start" });

    this.setState({
      location: item.location,
    });
}
posted @ 2022-07-14 10:30  进阶的哈姆雷特  阅读(16)  评论(0编辑  收藏  举报