Answers for "how to hide button after scrolling to top in react"

0

how to hide button after scrolling to top in react

function FunctionalComponent() {
const hasWindow = typeof window !== 'undefined'
const [opacity, setOpacity] = useState(1)

function handleElementsOnScroll() {
    window.onscroll = () => {
      let currentScrollPos = window.pageYOffset

      if (currentScrollPos > 600) {
        setOpacity(1)
      } else {
        setOpacity(0)
      }
    }
  }

  useEffect(() => {
    if (hasWindow) {
      handleElementsOnScroll()
    }
  }, [hasWindow])

    return (
        <button
          className="top-scroll"
          style={{ opacity }}
        >
          <i className="fa fa-arrow-up"></i>
        </button>
  )
}
Posted by: Guest on July-09-2021

Code answers related to "how to hide button after scrolling to top in react"

Code answers related to "Javascript"

Browse Popular Code Answers by Language