问题1:


单页应用开发时,当在A页面滚动滚动条后,点击进入B页面,滚动条保持在A页面的滚动位置。

目标:


同级路由切换时,滚动条回滚到页面顶端。

解决方案:


//在componentDidMount周期添加scrollTo()

class Home extends Component{
    componentDidMount(){
        window.scrollTo(0, 0);
    }
}

问题2:


在同一页面中点击回到顶部button,实现页面回到顶部.

目标:


onClick时,页面回到顶部。

解决方案:


<div onClick={() => {document.documentElement.scrollTop = 0;}}></div>

来源:https://segmentfault.com/a/1190000017439805

相关文章:

  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
  • 2021-10-23
  • 2021-10-05
  • 2021-10-06
猜你喜欢
  • 2022-12-23
  • 2021-05-17
  • 2021-12-12
  • 2022-12-23
  • 2021-08-01
  • 2021-08-26
  • 2021-09-10
相关资源
相似解决方案