AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Framer motion scroll example11/11/2023 In order to animate the box element, we need to make it a motion component.Animations, when done right, are powerful. Inside our component is a div element with the className: box. You’ll get an idea of how each Hook works a little later in this tutorial. These are the essential Hooks we’ll need to animate our box component. Import from "react-intersection-observer" useInView Hook from react-intersection-observer.motion and useAnimation Hooks from Framer Motion.Next, we’ll import everything else that’s required to create animation from the libraries we installed earlier: *imported Box component*/ /*imported Box component*/ We’ll animate this main component when it enters the viewport. Next, we’ll set up a demo app and will use Framer Motion and the react-intersection-observer library to identify when the elements are in view and then apply an animation.įirst, we’ll create a box component (this could be a or card, modal, or anything else) and import it into the main component, App.js. Next, we’ll install Framer Motion and react-intersection-observer: npm i react-intersection-observer framer-motion We’ll start by installing React: npx create-react-app my-app Now, let’s set up a simple React project and install the necessary dependencies. React-intersection-observer is a relatively small package, so there’s no need to worry about the overhead it may add to your project. This library provides Hooks and render props that make it easy to track the scroll position of elements on the viewport. We’ll be using the react-intersection-observer library, which is a React implementation of the intersection observer API. This is the approach that we’ll follow in this article. This JavaScript API provides a way to asynchronously observe changes in the intersection of a target element with a top-level document viewport.Īccording to the documentation, this API registers a callback function that is executed whenever an element we want to monitor enters or exits another element or enters or exits the viewport.Īlternatively, we can use a library that is designed to handle this functionality. We can build this functionality from scratch as a custom Hook using the Intersection Observer API. To address this, we’ll need to implement an intersection observer functionality that will prevent an element from mounting until its scroll position is in the viewport. It doesn’t have inbuilt functionality for animating elements based on their scroll position on the viewport. What is intersection observer functionality?įramer Motion animates elements when they mount on the DOM. The properties defined in the object are animated when the component mounts. animate takes in an object with the defined properties of the components to be animated. The motion function is used to create motion components, and these are the building blocks of Framer Motion.īy prefixing motion to a regular HTML or SVG element, the element automatically becomes a motion component: Motion ComponentĪ motion component has access to several props, including the animate prop. Motion and uaeAnimation are two styles that are triggered and controlled by functions exposed by Framer Motion. Framer Motion takes a different approach, by animating elements under the hood with preconfigured styles. Some React animations libraries, like react-transition-group and transition-hook, animate elements with manually configured CSS transitions. It provides production-ready animations and a low-level API to help simplify the process of integrating animations into an application. What is Framer Motion?įramer Motion is an animation library for creating declarative animations in React. Let’s start with a little background on Framer Motion and intersection observer functionality. This article will introduce the library’s basic concepts and build on those in the demonstration portion. There’s no need to have any prior knowledge of Framer Motion. Working knowledge of CSS properties such as opacity, transition, and scale.
0 Comments
Read More
Leave a Reply. |