In my angular project, I am currently adding animations to text. However, I want to make these animations dynamic.
For instance, I have a CSS class called .slide-in-top
with an animation time set to 1.3s
. Instead of this static value, I want to be able to set the animation time dynamically from a function like addAnimation()
, allowing me to choose between 2, 3 or 4 seconds
.
Furthermore, I also wish to customize the values within the keyframes
. Currently, the value is set to transform: translateY(-40px)
, which is static. I intend to modify this value dynamically through the addAnimation()
function, making it possible to select values like -30px or -50px
.
addAnimation();
function addAnimation(){
$("#user_text").addClass('slide-in-top');
}
.slide-in-top {
-webkit-animation: slide-in-top 1.3s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
animation: slide-in-top 1.3s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
}
@-webkit-keyframes slide-in-top {
0% {
-webkit-transform: translateY(-40px);
transform: translateY(-40px);
opacity: 0;
}
100% {
-webkit-transform: translateY(0);
transform: translateY(0);
opacity: 1;
}
}
@keyframes slide-in-top {
0% {
-webkit-transform: translateY(-40px);
transform: translateY(-40px);
opacity: 0;
}
100% {
-webkit-transform: translateY(0);
transform: translateY(0);
opacity: 1;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="margin-top: 20px; background:#0095ff;height:100px;padding:20px">
<p id="user_text">This is Animated text</p>
</div>