Think about your animations as being in two different states. One is your finished state, the other is your starting state. Exactly what these states are depends on what you want to animate. If you want elements to fade in, then the starting state is invisible and the finished state is visible. If you want elements to move in from the left, then the starting state is far off the page to the left, and the finished state is whatever you’ve specified as their position. Any time you want to animate an element, you need to make sure that it’s first in its starting state.
Now, you’ve pre-written some elements on the page where data will go (#result
1-5). These will need a starting state and a way to reset each element back to that state. I think your clearPage
function would be a good spot for that. When you’re filling in those elements, you should be able to animate each one in, bringing it to its finished state.
Check out jQuery’s documentation on effects. Each effect has a counterpart, ie. fadeOut()
and fadeIn()
. This will make it really easy to get to the starting and finished state.
Later on, you’ll learn some tools and techniques to generate elements rather than hard coding them on your page. I would not encourage you to stop what you’re doing to pursue a template or front end library. Just do the best you can with what you’ve got, but know that there’s a lot of cool things in your future. In fact, generating a page from data is the hottest topic in front end development, and a lot of the big names you may have heard about - React, Angular, Vue - are different ways to tackle this exact problem. There are other, simpler options, too.
Here’s a simple-ish project I did a while ago that makes use of HandlebarsJS for templating. It’s very light weight compared to React et al. Have a peek:
The only parts I want you to really read start on line 14 of the HTML section and lines 107, 151 of the JavaScript. You’ll notice that the HTML seems really small for the amount of data that’s displayed on the page. Line 14 defines a template for each post.
<!-- line 14 -->
<template id="post-template">
<li class="row posts">
<a href="https://reddit.com{{permalink}}" class="col-xs-2">
<img src="{{thumbnail-link thumbnail}}" alt="Thumbnail for the post {{title}}" class="thumbnail img-responsive"/>
</a>
<span class="info col-xs-offset-3 col-xs-6">
<h2>{{{title}}}</h2>
<p class="author">by {{author}}</p>
</span>
</li>
</template>
The stuff surrounded by double curly braces {{}}
are spots that will be filled in by the data provided by Reddit. Notice that I am not writing this out for each post I want to display. The JavaScript will handle that.
Line 107 of the JavaScript gets this template has a string. It passes this string to the Handlebars library which is what will replace all of those curly braced variables with the data.
// line 107
const postTemplate = Handlebars.compile($("#post-template").html());
postTemplate
is a function that will accept data and return the markup.
Much later, after I make an AJAX call to Reddit and receive a good response, I can give this template some data.
// line 151
response.posts.forEach(post => $posts.append(postTemplate(post))); // << using the postTemplate function
Simply, for each post in the response I got, give the post to postTemplate
and append the output to the page at $posts
(I define this earlier in the script, but that’s not important). Now, if I get 1, 5, 23, or 100 posts, it will work and I won’t have to change a thing about my HTML. I can also clear out all of the posts just by setting the container element’s HTML.
$posts.html('');
Again, this is just meant to show you a slice of the future. If you want to start with Handlebars, there are some Youtube tutorials that are quite good and it’s relatively easy to pick up. It’s not something most companies are looking for, though, so it’s not going to improve your chances of being hired and you’ll probably forget it all when you learn React.