Here's the working result.
var notificationTimer = 2000; // miliseconds to hide the notification alert
$('.submit-button').on('click', function () {
$('.notification-alert').addClass('notification-alert--shown');
setTimeout(function () {
$('.notification-alert').removeClass('notification-alert--shown');
}, notificationTimer)
});
.notification-alert {
color: white;
bottom: 20px;
right: 20px;
position: fixed;
background-color: #f4b251;
border-bottom: 4px solid #E89F3C;
color: #fff;
padding: 20px;
border-radius: 14px;
transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275);
transform: translateX(100%);
transition: all 500ms;
opacity: 0;
z-index: -1;
}
.notification-alert--shown {
opacity: 1;
transform: translateX(0);
transition: all 500ms;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="submit-button">show notification</button>
<div class="notification-alert">
<span>Great job, you're under way!</span>
</div>
I changed a few things:
- I changed
notification-alert
to a class because it's good practice to use classes for CSS transitions because the classes represent different states that your element will transition to and from.
- Additional, I added a new class
notification-alert--shown
which represents the state of this element when it is shown.
- I used jquery to add the class
notification-alert--shown
when the button is clicked to initiate the transition.
Now we need to explain how CSS transitions work:
CSS transitions work by adding frames to and from different CSS classes (as a good practice). In order to make a transition happen, you need at least two classes. In this case, that's notification-alert
and notification-alert--shown
. These classes only differ (i.e. have conflicting properties) with the properties transform
and opacity
.
Since those properties are different between the two classes, I can tell CSS to transition (that is, add frames between) the two classes when they change.
So when I add the class notification-alert--shown
, I am telling the browser to add frames to transition from the state {opacity: 0; transform: translateX(100%);}
to the new state {opacity: 1; transform: translateX(0);}
And I tell the browser that I want to transition all the different property by adding all
to transition: all ...
.
You also brought up a few questions in the comments:
This working great. But it's only show fade effect. But why this kind of animation isn't working?transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275);
And this leads me to think you have a slight misunderstanding about the transition-timing-function
property. The transition-timing-function
is not associated with how the element move exactly--it is only concerned with how the transition you asked for is applied over time. You can read more about transition timing property here.
And you also asked for one more thing:
Looks great now! How can I fade out after few seconds?
To do that, you can use window.setTimeout
to run some code to remove the class after a certain amount of time.
I've updated the example to demonstrate.
Here is a good tutorial on CSS transitions on CSS-tricks.