Skip to content

Instantly share code, notes, and snippets.

@macro6461
Last active April 12, 2020 15:58
Show Gist options
  • Save macro6461/b144e95dddbdd692addf968d3e8a4dda to your computer and use it in GitHub Desktop.
Save macro6461/b144e95dddbdd692addf968d3e8a4dda to your computer and use it in GitHub Desktop.
Code for react scroll arrow
App {
text-align: center;
height: 5000px;
}
.scrollTop {
position: fixed;
width: 100%;
bottom: 20px;
align-items: center;
height: 20px;
justify-content: center;
z-index: 1000;
cursor: pointer;
animation: fadeIn 0.3s;
transition: opacity 0.4s;
opacity: 0.5;
}
.scrollTop:hover{
opacity: 1;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 0.5;
}
}
import React, {useState} from 'react';
import {FaArrowCircleUp} from 'react-icons/fa';
import '../App.css';
const ScrollArrow = () =>{
const [showScroll, setShowScroll] = useState(false)
const checkScrollTop = () => {
if (!showScroll && window.pageYOffset > 400){
setShowScroll(true)
} else if (showScroll && window.pageYOffset <= 400){
setShowScroll(false)
}
};
const scrollTop = () =>{
window.scrollTo({top: 0, behavior: 'smooth'});
};
window.addEventListener('scroll', checkScrollTop)
return (
<FaArrowCircleUp className="scrollTop" onClick={scrollTop} style={{height: 40, display: showScroll ? 'flex' : 'none'}}/>
);
}
export default ScrollArrow;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment