最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to scroll the document before a list of divs is rendered on the screen? - Stack Overflow

programmeradmin0浏览0评论
<div key={"parent-div"}>
  <div key={"child-div-1"}></div>
  <div key={"child-div-2"}></div>
  <div key={"child-div-3"}></div>
  <div key={"child-div-4"}></div>
  <div key={"child-div-5"}></div>
</div>

The above React component will overflow the viewpoint.

Task: before the component is rendered on the screen, I want child-div-3 to be scrolled to the top.

If the parent div is rendered on the screen first and then the scrolling takes place, then there will be a slight visual glitch. This is what I want to prevent.

<div key={"parent-div"}>
  <div key={"child-div-1"}></div>
  <div key={"child-div-2"}></div>
  <div key={"child-div-3"}></div>
  <div key={"child-div-4"}></div>
  <div key={"child-div-5"}></div>
</div>

The above React component will overflow the viewpoint.

Task: before the component is rendered on the screen, I want child-div-3 to be scrolled to the top.

If the parent div is rendered on the screen first and then the scrolling takes place, then there will be a slight visual glitch. This is what I want to prevent.

Share Improve this question edited Feb 4 at 4:46 Phil 165k25 gold badges262 silver badges267 bronze badges asked Feb 4 at 4:32 Bear Bile Farming is TortureBear Bile Farming is Torture 5,1318 gold badges41 silver badges118 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

You want to run something before the screen is actually painted, but you need the result of rendering before you do this. You can use useLayoutEffect.

  1. Create a ref using useRef pointing to the div.
  2. Use scrollIntoView to scroll to the div in an effect. (Do this before paint)

You can also create a hook for this:

const useScrollToDivTop = () => {
  const divRef = useRef();

  useLayoutEffect(() => {
    divRef.current.scrollIntoView();
  }, []);

  return { divRef };
};

export default function App() {
  const { divRef } = useScrollToDivTop();
  return (
    <div key={"parent-div"} className="parent-div">
      <div key={"child-div-1"}>1</div>
      <div key={"child-div-2"}>2</div>

      <div ref={divRef} key={"child-div-3"}>
        3
      </div>
      <div key={"child-div-4"}>4</div>
      <div key={"child-div-5"}>5</div>
    </div>
  );
}

Sandbox

发布评论

评论列表(0)

  1. 暂无评论