Utilize a class within a Framer Motion element to incorporate animations or set initial properties

Is there a way to apply a class using Framer Motion tag in the animate and initial props? Here's an example:

<motion.div
  initial={{ className: 'hidden' }}
  animate={{ className: 'visible' }}
>
  <div>yo</div>
</motion.div>

I need to use classes because I am incorporating tailwindcss into my project.

The issue is, when I try the method above, it just adds style="class-name:{class_name}" in the generated HTML tag, which has no effect. Any suggestions?

Thank you in advance.

Answer №1

If you want to incorporate vanilla JavaScript with event handling, you can utilize the following methods:

onAnimationStart()

and

onAnimationComplete()

After implementing these event listeners, proceed with your logic accordingly. For more information, please visit

Answer №2

To manipulate only visibility, you can experiment with CSS properties such as

<motion.div
  initial={{ display: "none" }}
  animate={{ display: "block" }}
>
  <div>hey</div>
</motion.div>

Furthermore, you have the option to utilize callback functions and JavaScript selectors as mentioned by @Arfan:

function onStart() {
  document.getElementById("target")?.classList.add("visible");
  document.getElementById("target")?.classList.remove("hidden");
}
function onEnd() {
  document.getElementById("target")?.classList.remove("visible");
  document.getElementById("target")?.classList.add("hidden");
}
...
<motion.div
  initial="rest"
  animate="anim"
  onAnimationStart={onStart}
  onAnimationComplate={onEnd}
  id="target"
>
  <div>hey</div>
</motion.div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React components cannot be updated while in an unmounted state, unless utilizing a stateless component

What if I were to assume I understand the error and its usual cause, but now I'm at a loss as to why React is complaining? This error seems to be originating from a simple component: export interface PostHandlerOutProps { posts: PostFragment[]; } ...

Differences between material-ui tables and HTML tables [ WARNING: Invalid element type]

What could be causing the error message: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. It's interesting that I encounter this error when using the material-u ...

Combining D3.js tooltip positioning with CSS overflow styling

I am encountering an issue with displaying tooltips in D3 when combined with CSS overflow. Is there a solution available for this particular problem? CSS code: #chart1 { overflow-x: auto; overflow-y: hidden; max-width: 800px; max-height: 22 ...

What is the best way to create individual Menu items in a React/MUI navigation bar?

I am currently developing a navigation bar for an application, and I seem to be facing an issue with differentiating between multiple Menu/MenuItem elements. No matter what approach I take, both Menus and their respective MenuItems end up directing to the ...

Identifying elements using xpath - Streamlined Xpath Techniques

I successfully found the element using the xpath copied directly from the code. Can someone assist me in creating a simpler xpath for the following code snippet, which is fully functional! WebElement oCheckbox = myDriver.findElement(By.xpath(".//*[@id=&ap ...

How can you trigger a modal to appear when an image is clicked?

There are images inside <div> tags. When one of the images is clicked, a modal appears to display information about the specific image. Each modal has a unique id or class, such as modal4. My example: <image type="image" src="Images/Drake.jpg" ...

Can React-Select be utilized in the browser directly from the CDN?

Is it possible to utilize react-select directly in the browser without using bundlers nowadays? The most recent version that I could find which supports this is 2.1.2: How to import from React-Select CDN with React and Babel? In the past, they provided r ...

Choose the content in the second column

Can anyone help me with CSS selectors? I've been trying to target the second .col_content element in my code to apply a specific background image, but I haven't had any luck so far. Adding a second class would be an easy fix, but I'm curious ...

Using the starting values obtained from the state of the material-ui dropdown within a redux-form

I have a situation where I need to initialize the value of a material-ui dropdown menu within a redux-form. To achieve this, I am fetching the desired value [exercise.exercise_type_id and exercise.exercise_type_name] as well as the available options [types ...

The menu item fails to respond to clicks when hovering over the header background image

I'm having an issue with the Menu Link not working. I can click on the menu item when it's placed inside the body, but when I try to place it over the background header image, it stops working. Any help would be greatly appreciated. <div clas ...

Looking to position the Secondary Navigation Bar on squarespace at the bottom of the page, distinct from the primary Navigation Bar

When SALES PAGE becomes the secondary navigation option Instructions for positioning style to Bottom Center I am attempting to place it at the bottom of the navigation bar. Can you provide me with the necessary code or styles in squarespace? When I choose ...

The state of a ReactJS component is failing to update whenever a value within the state array is altered

In my React component, I'm facing an issue where I want the state to update automatically without refreshing the page. I've tried using lodash's isEqual method to compare state changes, but it's not updating the state as expected. Here ...

Text that doesn't appear overwhelming in a Framer Motion text reveal animation

As a newcomer to Framer Motion, I recently attempted to create a Stagger Text effect and a Text Reveal Animation for my portfolio based on various articles. However, despite trying multiple examples, I have encountered an issue where the animation occurs s ...

`Set the body height to equal that of the entire document`

Currently facing an issue on the test page when scrolling down: The body element is set to have a height of 100%, but it only takes up the viewport's height, not the entire document height. The goal is for the page to always display at 100% of the d ...

Is GetStaticPath in NextJS suitable for generating large amounts of static data?

On my website, I have a page dedicated to displaying information about hotels based on their unique IDs. To achieve this functionality, I utilize getStaticPath to generate paths like /hotel-info/542711, where 542711 represents the hotel's ID. However ...

Unable to designate "OrderedDict()...: "..."" needs to be a "..." form

I am currently using Django Rest & React to develop a web application. models.py class Bike(models.Model): RegoNumber = models.CharField(max_length=10) RegoExpiry = models.DateField(auto_now=False,auto_now_add=False) LastRentedDate = models. ...

Modify content or display picture within accordion panel based on its identifier

In my view, I have a model representing a list of items. Each item has a unique ID, which is included in the H2 header tag. The details of each item are displayed in a div below the header. Initially, there is an image within the header that is set to disp ...

Error: The session client failed to fetch due to a connection refusal in NextAuth v3

Currently, I am attempting to execute the production build on my local machine which is why I am utilizing localhost:3000 as the next URL. Upon running this setup, I encounter the following error in the terminal: https://next-auth.js.org/errors#client_fetc ...

Error 404: Django failing to load image on the webpage

Just starting out with Django and I've hit a snag trying to load a static file (image). Here's the error message I'm getting: 127.0.0.1/:1707 GET http://127.0.0.1:8000/static/css/static/images/background/1.jpg 404 (Not Found) I suspect th ...

Unexpected issue encountered while executing create-react-app command in Mac terminal

I've encountered an unusual error while trying to run create-react-app on my Mac today. Despite attempting various solutions such as updating node, Yarn, eslint, CRA, and clearing the node cache, I still can't seem to resolve it. What could be th ...