📜  React 粘钩 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:41.253000             🧑  作者: Mango

代码示例1
import React, { Fragment, useEffect, useRef, useState } from 'react';
import Sticky from './sticky';

export default () => {
  const [isSticky, setSticky] = useState(false);
  const ref = useRef(null);
  const handleScroll = () => {
    if (ref.current) {
      setSticky(ref.current.getBoundingClientRect().top <= 0);
    }
  };

  useEffect(() => {
    window.addEventListener('scroll', handleScroll);

    return () => {
      window.removeEventListener('scroll', () => handleScroll);
    };
  }, []);

  return (
    
      

Lorem ipsum...

Lorem ipsum...

); };