PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 8, 2022

[FIXED] How to animate an element as it leaves a page?

 November 08, 2022     animation, css, html, menu, reactjs     No comments   

Issue

I am currently designing a menu that slides in on the right when you click the menu button, and I want it to slide out when you click the X.

so far I have the slide in animation down, so as soon as it renders, this animation occurs:

@keyframes slideInFromRight {
    0% {
      transform: translateX(50%);
    }
    100% {
      transform: translateX(0);
    }
}

is there any way for me to apply this animation (sliding out) to the div when the user hits X and the item is no longer rendered on the page:

@keyframes slideInFromRight {
    0% {
      transform: translateX(0);
    }
    100% {
      transform: translateX(50%);
    }
}

Not sure if this has any relevance but, For the rendering of the menu, i am using the useState() react hook to decide when to render in the sidebar menu or not.


Solution

You should use state variable to change className, for example,

import React, {useState} from 'react';

function index() {
  const [slideEffect, setSlideEffect] = useState(false);
  const slide = () => setSlideEffect(!slideEffect);

  return <div className=slideEffect?"slideRight":"slideLeft">Element</div>;
}

export default index;

Also you can use animate css https://animate.style/ install it via npm,

npm install animate.css --save

and change the code I wrote like this

return <div className={`animate__animated ${slideEffect? "animate__fadeOutRight":"animate__fadeInRight"}`}>Element</div>;


Answered By - yusuf_sabri
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing