Last active
April 12, 2020 15:58
-
-
Save macro6461/b144e95dddbdd692addf968d3e8a4dda to your computer and use it in GitHub Desktop.
Code for react scroll arrow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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