React fade in element
An example using css class toggling + opacity transitions:
https://jsfiddle.net/ybktodLc/
Here's the interesting CSS:
.basket {
transition: opacity 0.5s;
opacity: 1;
}
.basket.hide {
opacity: 0;
pointer-events:none;
}
And the render function:
render() {
const classes = this.state.open ? 'basket' : 'basket hide'
return(
<div className="basket">
<button className="basketBtn" onClick={this.handleDropDown}>
{this.state.open ? 'Close' : 'Open'}
</button>
<BasketContents className={classes}/>
</div>
)
}
React - fade in div, pause and fade out div
Hey I have edited your sandbox to achieve the result you desire:-
Changes:-
1) Added show
and hide
classes.
2) Introduced a boolean state for transition rather than depending on text because your message-banner
div doesn't have its own height or width. We will simply let the text stay but hide the div away from the user.
3) Instead of animation
, used transition
since you're simply toggling between two states and you want to stick with those for rest of your application. With animation, you will have to do some more tricks for them to stick. Plus animation
is useful for more complex scenario.
How to fade-out and fade-in in React?
Working demo
Add transition="0.8s linear"
property to the root element(box) to make color transition smoother.
<Box
bg={color}
h="100%"
display="flex"
flexDir="column"
justifyContent="center"
transition="0.8s linear">
</Box>
Add show and hide classname based on the opacity state className={ opacity ? "show" : "hide"}
.
<Box
className={ opacity ? "show" : "hide"}
>
<Flex>
<Box>
<Icon as={FaQuoteLeft} w={7} h={6} color={color} />
<Text fontSize="2xl" color={color} pl={8} mt="-20px">
-{quote.quote}
</Text>
</Box>
</Flex>
</Box>
<Box>
<Text className={ opacity ? "show" : "hide"} fontSize="xl" align="right" color={color}>
-{quote.author}
</Text>
</Box>
Add the below-given styles to apply animation based on opacity state:
.hide {
-webkit-animation: fadeinout .3s linear forwards;
animation: fadeinout .3s linear forwards;
}
@-webkit-keyframes fadeinout {
0% { opacity: 0.6; }
50% { opacity: 0.2; }
100% { opacity: 0; }
}
@keyframes fadeinout {
0% { opacity: 0.6; }
50% { opacity: 0.2; }
100% { opacity: 0; }
}
.show {
-webkit-animation: display .5s linear forwards;
animation: display .5s linear forwards;
}
@-webkit-keyframes display {
0% { opacity: 0.2; }
50% { opacity: 0.6; }
100% { opacity: 1; }
}
@keyframes display {
0% { opacity: 0.2; }
50% { opacity: 0.6; }
100% { opacity: 1; }
}
you can also adjust transition time based on your requirements.
TailwindCSS fade in Element on click
The solution is, you need to add duration, like this:
`transition-all duration-200 ${fade ? "opacity-100" : "opacity-0"}`
Here is my forked sandbox you had given, I've removed extra inline CSS, so it may become evident.
Here is the complete code:
function Header() {
const [popCard, setPopCard] = useState("hidden");
const [fade, setFade] = useState(false);
const handleMenuClick = () => {
setPopCard("inline-block");
setFade(true);
};
const handleXClick = () => {
setPopCard("hidden");
setFade(false);
};
console.log(fade, "fade");
return (
<div className="text-center">
<header className="sticky z-50 top-0 shadow-md bg-white border-b p-5">
<div className="flex justify-between items-center">
<h1 className="text-6xl text-red-500 cursor-pointer">Velvet</h1>
<button
className="text-3xl border rounded-lg px-5"
onClick={handleMenuClick}
>
Menu
</button>
</div>
</header>
<div className="p-10">
<div
className={`transition-all duration-200 ${
fade ? "opacity-100" : "opacity-0"
}`}
>
<div className="flex justify-end">
<button className="mt-2 mr-2 border p-2" onClick={handleXClick}>
Close
</button>
</div>
<div className="space-y-2 text-3xl text-center mt-5 mb-10 mx-5 text-red-500">
<h1>Kontakt</h1>
<h1>O Velvetu</h1>
</div>
</div>
</div>
</div>
);
}
export default Header;
Sandbox: https://codesandbox.io/s/sweet-swartz-mr3nru?file=/pages/index.js:41-1396
How to wait and fade an element out?
May 2021 update: as tolga and Alexey Nikonov correctly noted in their answers, it’s possible to give away control over how long the alert is being shown (in the original question, 2 seconds) to the transition-delay
property and a smart component state management based on the transitionend
DOM event. Also, hooks are these days recommended to handle component’s internal state, not setState
. So I updated my answer a bit:
function App(props) {
const [isShowingAlert, setShowingAlert] = React.useState(false);
return (
<div>
<div
className={`alert alert-success ${isShowingAlert ? 'alert-shown' : 'alert-hidden'}`}
onTransitionEnd={() => setShowingAlert(false)}
>
<strong>Success!</strong> Thank you for subscribing!
</div>
<button onClick={() => setShowingAlert(true)}>
Show alert
</button>
(and other children)
</div>
);
}
The delay is then specified in the alert-hidden
class in CSS:
.alert-hidden {
opacity: 0;
transition: all 250ms linear 2s; // <- the last value defines transition-delay
}
The actual change of isShowingAlert
is, in fact, near-instant: from false to true, then immediately from true to false. But because the transition to opacity: 0 is delayed by 2 seconds, the user sees the message for this duration.
Feel free to play around with Codepen with this example.
Since React renders data into DOM, you need to keep a variable that first has one value, and then another, so that the message is first shown and then hidden. You could remove the DOM element directly with jQuery's fadeOut
, but manipulating DOM can cause problems.
So, the idea is, you have a certain property that can have one of two values. The closest implementation is a boolean. Since a message box is always in DOM, it's a child of some element. In React, an element is result of rendering a component, and so when you render a component, it can have as many children as you want. So you could add a message box to it.
Next, this component has to have a certain property that you can easily change and be completely sure that, as soon as you change it, the component gets re-rendered with new data. It's component state!
class App extends React.Component {
constructor() {
super();
this.state = {
showingAlert: false
};
}
handleClickShowAlert() {
this.setState({
showingAlert: true
});
setTimeout(() => {
this.setState({
showingAlert: false
});
}, 2000);
}
render() {
return (
<div>
<div className={`alert alert-success ${this.state.showingAlert ? 'alert-shown' : 'alert-hidden'}`}>
<strong>Success!</strong> Thank you for subscribing!
</div>
<button onClick={this.handleClickShowAlert.bind(this)}>
Show alert
</button>
(and other children)
</div>
);
}
}
Here, you can see that, for message box, either alert-shown
or alert-hidden
classname is set, depending on the value (truthiness) of showingAlert
property of component state. You can then use transition
CSS property to make hiding/showing appearance smooth.
So, instead of waiting for the user to click button to show the message box, you need to update component state on a certain event, obviously.
That should be good to start with. Next, try to play around with CSS transitions, display
and height
CSS properties of the message box, to see how it behaves and if the smooth transition happening in these cases.
Good luck!
PS. See a Codepen for that.
ReactJS: Fade in div and fade out div based on state
Just use a conditional class
and CSS.
Have a state
variable like visible
.
this.state = {
visible:false
}
And for the other inputs do something like
<input className={this.state.visible?'fadeIn':'fadeOut'} />
So depending upon the state.visible
the input will have a class
of either fadeIn
or fadeOut
.
And then just use simple CSS
.fadeOut{
opacity:0;
width:0;
height:0;
transition: width 0.5s 0.5s, height 0.5s 0.5s, opacity 0.5s;
}
.fadeIn{
opacity:1;
width:100px;
height:100px;
transition: width 0.5s, height 0.5s, opacity 0.5s 0.5s;
}
So every time the state.visible
changes the class
changes and the transition
takes place. The transition
property in CSS is basically all the transitions separated by commas. Within the transition the first argument is the property to be modified (say height
, width
etc), second is transition-duration
that is the time taken for the transition and third(optional) is transition-delay
ie how much time after the transition has been initiated does the transition for the particular property take place. So when this.state.visible
becomes true
the .fadeIn
class is attached to the object. The transition
has height
and width
taking 0.5s each so that will take 0.5s to grow and after it is finished the opacity
transition (which has a delay of 0.5s) will trigger and take a further 0.5s to get opacity
1. For the hiding it's the reverse.
Remember to have the OnClick
event on the button handle the changing of this.state.visible
.
Related Topics
JavaScript Invalidcharactererror When Modifying a CSS Name with a Space
Check Visibility of an Object with JavaScript
Get Document.Stylesheets by Name Instead of Index
Custom CSS Properties, Why Not
Find JavaScript Code That Changes Particular CSS Properties
Angular: Why Doesn't CSS Justification Work with Ng-Repeat
How to Reasonably Animate Font Awesome Lock Icons
Swap Style Sheet with Localstorage
How Much of an Effect Can Different Operating Systems Have on Displaying Web Pages
Modify CSS Classes Using JavaScript
Getting the Rgb Values for a CSS/HTML Named Color in JavaScript
Using !Important in Jquery's CSS() Function
How to Apply D3.Js Svg Clipping After Zooming
Jquery.Lazy(): Plugin Is Not Loading My 'Li' Contents
Highcharts.Js - Background Color of Axis Only
Is JavaScript Execution Deferred Until CSSom Is Built or Not