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 :: how to remove comma from array in javascript 
Javascript :: javascript isalphanumeric 
Javascript :: javascript get element by multiple class names 
Javascript :: javascript replace dash with space 
Javascript :: How to hthe amount of users online in discordjs 
Javascript :: js remove li from ul 
Javascript :: how to merge two sorted arrays in javascript 
Javascript :: set js 
Javascript :: typescript read json file 
Javascript :: javascript canvas reset transform 
Javascript :: Change the HTML of an element 
Javascript :: get element by id in jquery 
Javascript :: What is the Difference between Undefined, undeclared, Null 
Javascript :: async false in ajax 
Javascript :: Reverse numbers from an array in javascript 
Javascript :: get table row data jquery 
Javascript :: react add link to button 
Javascript :: sanitizing user input javascript 
Javascript :: regex exact match case insensitive 
Javascript :: change node version 
Javascript :: jwt token expire time in node js 
Javascript :: reset function javascript 
Javascript :: onselect javascript 
Javascript :: javascript set class of element 
Javascript :: jquery wait for all ajax requests to complete 
Javascript :: jquery insert after element 
Javascript :: google sheets api javascript 
Javascript :: react function being called every minute 
Javascript :: js get url variables 
Javascript :: set background color dynamically javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =