如何在底部滚动以对按钮做出反应

Jon*_*as 2 javascript ecmascript-6 reactjs react-redux

当用户单击按钮时,我试图在底部向下滚动用户。我真的很努力,但没有找到任何解决方案。有人可以帮助我如何实现我的目标。

谢谢

小智 5

您可以使用document.body.offsetHeight获取页面的高度并使用 滚动到它windows.scroll。如果需要支持IE11及以下使用window.scroll(0, document.body.offsetHeight);

import React from 'react';

function App() {

  function handleScroll() {
    window.scroll({
      top: document.body.offsetHeight,
      left: 0, 
      behavior: 'smooth',
    });
  }

  return <button type="button" onClick={handleScroll}>Scroll</button>;

}
Run Code Online (Sandbox Code Playgroud)