Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react scrollTop smooth

import React, { useEffect, useState } from "react";

export default function ScrollToTop() {
  const [isVisible, setIsVisible] = useState(false);

  // Top: 0 takes us all the way back to the top of the page
  // Behavior: smooth keeps it smooth!
  const scrollToTop = () => {
    window.scrollTo({
      top: 0,
      behavior: "smooth"
    });
  };

  useEffect(() => {
    // Button is displayed after scrolling for 500 pixels
    const toggleVisibility = () => {
      if (window.pageYOffset > 500) {
        setIsVisible(true);
      } else {
        setIsVisible(false);
      }
    };

    window.addEventListener("scroll", toggleVisibility);

    return () => window.removeEventListener("scroll", toggleVisibility);
  }, []);

//scroll-to-top classes: fixed, bottom:0, right:0
  return (
    <button
      type="button"
      className={`scrollToTop scroll-btn ${isVisible ? "show" : ""}`}
      onClick={scrollToTop} >
      <i className="far fa-arrow-up"/>
    </button>
  );
}
Comment

smooth scroll react

$ npm install react-scroll
Comment

PREVIOUS NEXT
Code Example
Javascript :: is odd javascript 
Javascript :: find multiples of a number 
Javascript :: how to make javascript function consise 
Javascript :: remove object if key is duplicate javascript 
Javascript :: for loop react 
Javascript :: view child with directive not working undefined 
Javascript :: map in js 
Javascript :: uirouter 
Javascript :: when programmers net goes down 
Javascript :: sort object with certain value at start of array js 
Javascript :: create multiple array buttons in javascript 
Javascript :: convert base64 to pdf file javascript 
Javascript :: js lambda 
Javascript :: object.keys javascript 
Javascript :: Different views for Desktop and mobile Angular 
Javascript :: crdit card input format 
Javascript :: onpress not working when textinput isfocused in react native 
Javascript :: while loops js 
Javascript :: async await 
Javascript :: mock click function functinal component enzyme 
Javascript :: expo location background example 
Javascript :: use axios cancel token in react.js useEffect 
Javascript :: node js error 
Javascript :: nodejs check if file is running on server or client 
Javascript :: auth provider react 
Javascript :: react loop return 
Javascript :: how to create instance of class in javascript 
Javascript :: pdf.js get current page number 
Javascript :: js insert after element 
Javascript :: mongodb find array with element 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =