99
loading...
This website collects cookies to deliver better user experience
npm install framer-motion
import "./styles.css";
export default function ScrollToTop() {
return (
<button
className="scrollToTop-btn"
>
Click Me!
</button>
);
}
.scrollToTop-btn {
display: flex;
justify-content: center;
align-items: center;
position: fixed;
bottom: 1.5rem;
right: 1.5rem;
width: 3.5rem;
height: 3.5rem;
font-size: 1rem;
line-height: 3rem;
background-color: #007acc;
border: none;
border-radius: 50%;
color: white;
cursor: pointer;
}
//...
const arrowUp = (
<svg
className="arrowUp" // <-- add this for styling
aria-hidden="true"
focusable="false"
data-prefix="fas"
data-icon="chevron-up"
role="img"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 448 512"
>
<path
fill="currentColor"
d="M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z"
></path>
</svg>
);
return (
<button
className="scrollToTop-btn"
>
{arrowUp} // <--- change here
</button>
);
}
width
and height
to the icon..arrowUp {
width: 2rem;
height: 2rem;
}
goToTop
function.goToTop
function scrolls into the beginning of the page, top: 0
, with a more natural and smooth behavior, behavior: "smooth"
.//...
// Add the smooth behavior to go to top
const goToTop = () => {
document.documentElement.scrollTo({
top: 0,
behavior: "smooth"
});
};
return (
<button
className="scrollToTop-btn"
onClick={goToTop} // <--- add this
>
{arrowUp}
</button>
);
}
npm install framer-motion
import { motion } from "framer-motion";
//...
return (
<motion.button // <--- change here
className="scrollToTop-btn"
onClick={goToTop}
>
{arrowUp}
</motion.button> // <--- and change here
);
}
//...
return (
<motion.button
className="scrollToTop-btn"
onClick={goToTop}
initial={{ y: 100, opacity: 0 }}
animate={{ y: 0, opacity: 1,
transition: { duration: 0.6 } }}
exit={{ y: 100, opacity: 0,
transition: { duration: 0.6 } }}
whileHover={{
scale: 1.2,
transition: { duration: 0.2 }
}}
whileTap={{ scale: 1 }}
>
{arrowUp}
</motion.button>
);
}
useState
and useEffect
from react.import { useState, useEffect } from "react";
scrollPosition
state that will have the position of the Y/height of the scrollbar./...
const [scrollPosition, setScrollPosition] = useState(0);
useEffect(() => {
const updatePosition = () => {
setScrollPosition(window.pageYOffset);
};
window.addEventListener("scroll", updatePosition);
return () => window.removeEventListener("scroll", updatePosition);
}, []);
//...
scrollPosition
statereturn (
{scrollPosition > 100 && (
<motion.button
//...
</motion.button>
)}
);
AnimatePresence
to be able to use the exit property. When we are at the top of the page, the button will exit gracefully.import { motion, AnimatePresence } from "framer-motion";
return (
<AnimatePresence>
{scrollPosition > 100 && (
<motion.button
//...
</motion.button>
)}
</AnimatePresence>
);