A simple, not fancy at all, animated text rotator.
Features include:
- The text list rotates every second.
- As the text rotates it's animated using CSS.
Built using:
- Vue
- CSS Transition and
@keyframes
Animation - Google Fonts - Luckiest Guy and Boogaloo
Play around with it on CodePen
Or see it live! here
Here are the general steps to create your own Not Fancy Text Rotator. Essentially, the CSS provides the animation and JavaScript is used to add or remove the class.
Alright, let's set up the JavaScript to add and remove the class. This is probably the most tricky part of the entire app. How do we rotate the class on each list item. Well, no worries, we can use setInterval
👍
mounted() {
setInterval(() => {
// 1. Let's look over our list
this.favorites.forEach(fav => {
// 3. We should reset the number to 0 once it reach the max list
if(fav.class === this.favorites.length) {
fav.class = 0;
}
// 2. We will increment our class number
fav.class++;
});
}, 1000); // 4. The class will change every 1 sec
},
And this is how my list of item looks like:
data: {
favorites: [
{name: 'vue', class: 1},
{name: 'css', class: 2},
{name: 'html', class: 3},
],
},
Ok, on to the heaving lifting 💪 Next, let's use CSS to place our words. We'll be using absolute positing.
/* Parent */
ul {
position: relative;
}
/* Child */
li {
position: absolute;
}
/* Manually arrange the child */
.w1 {
top: 0px;
}
.w2 {
top: 40px;
}
.w3 {
top: 80px;
}
Here is the fun part! Let's create our animation. There are 3 animation that we need to create:
- The first word needs to have the drop-in effect
- All the words in between must move 1 level down (except the first one).
- The last word will slide-off
.w1 {
/* Apply the animation */
animation-name: drop-in;
animation-duration: 0.5s;
/* You can also write on one line */
/* animation: drop-in 0.5s; */
}
@keyframes drop-in {
0% {
top: -80px;
opacity: 0;
}
50% {
opacity: 1;
}
100% {
top: 0;
}
}
li:not(.w1) {
transition: top 0.5s;
}
Very important in this step! Notice I'm using animation-delay
. This is because I want to slide to appear after the word has move 1 level down. By using the delay, the animation appears to happen one after the other -- or multi-step transition.
.w3 {
animation-name: slide-off;
animation-duration: 0.8s;
animation-delay: 0.8s; /* !!! */
}
@keyframes slide-off {
100% {
left: 1000px;
opacity: 0;
}
}
Finally, let's output our list to the HTML and bind our rotating class.
<ul>
<li v-for="(fav, index) in favorites"
:key="index"
:class="`w${fav.class}`"
>
{{ fav.name }}
</li>
</ul>
Challenge inspired by Urbanarium's site.
This is part of they #NotFancyAppChallenge. It's all about learning by doing. The only rule, it must be completed within 24hours. Anything more, it’s too fancy 😜 Join me!
More info here ➡️ Learn more about the challenge