Skip to content

Instantly share code, notes, and snippets.

@Houserqu
Created August 26, 2022 03:45
Show Gist options
  • Save Houserqu/011b051cf43a6d016543132114b8a1ea to your computer and use it in GitHub Desktop.
Save Houserqu/011b051cf43a6d016543132114b8a1ea to your computer and use it in GitHub Desktop.
前端UI相关

滚动吸顶的 js 实现

  useEffect(() => {
    const handle = () => {
      const top = document.documentElement.scrollTop || document.body.scrollTop
      setTabFixed((value: boolean) => {
        console.log(top, value, safeArea().top)
        if (top > headerRef.current?.clientHeight) {
          console.log('set fixed')
          return true
        }

        if (top < headerRef.current?.clientHeight) {
          return false
        }

        return value
      })

    }

    window.addEventListener('scroll', handle)

    return () => {
      window.removeEventListener('scroll', handle)
    }
  }, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment