Wednesday, November 30, 2022

Infinite Scroll Pagination With JavaScript and a REST API

Infinite Scroll Pagination With JavaScript and a REST API

Infinite scroll pagination is inspired by websites such as Facebook and Twitter. This is just pagination where as the user scrolls to the bottom of the page, more content is loaded. This improves user experience on the website by making sure there is always more content on the page for users to read.

Doing Infinite Scroll Pagination Right

When infinite scroll pagination is implemented, there are a few really important points to remember.

Important links should not be at the bottom of the page. This is because every time the user tries to scroll down to find them, a new set of entries will load. All important links should be fixed on a sidebar, or permanently kept on the top. 

2. Plan Ahead

It's important that you plan ahead: where you want to include pagination, and how you are going to process it. A common way of doing pagination is by listing the page numbers at the bottom of the page. Using the infinite scroll method however, no more page numbers will appear at the end of your article list, as they're no longer needed. This pagination can be use on all themes as long as you don't include loads of information in your footer section, as it may not give the desired effect.

In this tutorial, we are going to learn how to implement the infinite scroll feature in Javascript.

The page will display a list of fun facts about cats, which will come from an API. The API returns 10 fun facts by default. When you scroll to the bottom of the page, the application will display an indicator to denote the loading state of the app. Meanwhile, the app will call the API to load the next set of fun facts. 

We are going to use this URL, for loading fun facts. The API accepts a query string: page which tells the API which page to load.

Now, let's get started with the application.

1. Create the Project Structure

First, create a folder with the following structure.

2. Build the HTML File

We are going to have several sections in our HTML file. A container, where the whole scrollable list of fun facts will be rendered. A quotes section for every fun fact. And, there will be a loader, which will be visible when the fun facts are loading. The loader will be invisible by default.

3. Build the Script

Next, we need to create a script, which will connect with the div and load the fun facts. To achieve this, we will use the querySelector().

We also need few control variables to define which set of items will be shown on the screen. The control variables in this snippet of code are:

  • currentPage: The current page is initialised to 1. When you scroll to the bottom of the page, the current page will be incremented by 1 and an API request will be made to get the contents of the next page. When the page is scrolled to the top, the current page will be decremented by 1. 
  • total: This variable stores the total number of quotes returned by the Fun Facts API.

4. Build the getFacts function

The role of the getFacts function is to call the API, and return the fun facts. The getFacts function accepts a single argument: page. It uses the Fetch API mentioned above, to fetch data for the infinite scroll.

Fetch always returns a promise, hence we are going to use the await-async syntax for receiving and processing the response. To get the json data, we will use the json() function. The getFacts function would return a promise, which will resolve and return the JSON. 

5. Build the showFacts function

Now, that we have received the fun facts, where would we display these facts? This is why we need to have a showFacts function.  The showFacts function works by iterating through the facts array. Then, it uses the template literal syntax to create a HTML representation of a fact object

A sample of the generated blockFact element is:

We make use of the appendChild function to add the <blockfact> element to the container. 

6. Show and Hide the Loading Indicator

As the user reaches the end of the page, a loading indicator has to be shown. For this, we are going to introduce two function. One for loading, and the other for hiding the loader. We would use opacity: 1 to show the loader. And, opacity: 0 to hide the loader. Adding and removing opacity will show/hide the loader, respectively.

7. Check for More Fun Facts

To ensure on performance, we are going to introduce a function that can check if the API has more facts. The hasMoreFacts() function would return true if there are more items to fetch. If there are no more items to fetch, the API calls would stop.

8. Code the loadFacts function

The loadFacts function is responsible for performing four important actions:

  • show or hide the loading indicator
  • call getFacts function to fetch more facts.
  • show the facts

In a sense, a drawback of this implementation is how fast it runs. You will not see the loading indicator, most of the time, because the API can return very fast. If you want to see the loading indicator with every scroll, a setTimeout function can be used. Tweaking the delay of your setTimeout function will decide how long the loading indicator will be shown.

9. Handle Scroll Events

When the user scrolls to the bottom of the page a scroll event handler is required to call the loadFacts function. The function will be called if all the following conditions are met:

  • the scroll has reached the bottom of the page
  • there are be more facts to load

To achieve the scroll event, we will be making use of three window properties:

  • window.scrollHeight gives the entire document's height
  • window.scrollY gives an account of how far the document was scrolled by the user.
  • window.innerHeight gives the height of the window that is visible

The diagram below gives a better overview of the above properties. Also you will be able to understand that, if the sum of innerHeight and scrollY are equal too, or greater than scrollHeight, the end of the document is reached and this is when more fun facts have to be loaded.

10. Initialise the Page

The last step in our Infinite Scroll would be to initialise the page. It is important to call loadFacts, to load the very first set of fun facts.

Live Demo

Conclusion

Now, we have implemented a simple Infinite Scroll in Javascript, which will fetch and render fun facts about cats, whenever the user scrolls. This is just one of the most commonly used methods for infinite scrolling. 


Mastering Logo Design in Adobe Illustrator

Mastering Logo Design in Adobe Illustrator

Learn how to design a logo in Illustrator in this free course. Explore how to design your own logo with an experienced logo designer. We'll create a finished logo design by the end of the course!

Watch the Full Mastering Logo Design in Adobe Illustrator Course

What You Will Learn

  • How to design your own logo
  • How to design a logo in Illustrator
  • The basics of Illustrator for creative logo design
  • A look at the creative logo design process from a logo designer

About Your Instructor

Daniel WhiteDaniel WhiteDaniel White

1. Intro to Adobe Illustrator Logo Design

1.1 Introduction to Logo Design in Adobe Illustrator

Watch Video Lesson (1 min) ↗

Ready to explore Adobe Illustrator logo design? In the first half of this course, we're going to look at the essential tools in Adobe Illustrator that you need as a logo designer. In the second half, we'll use those techniques to build a logo together. At the halfway point, we'll pause for a Q&A session, originally held in this course's livestream.

Ready to get started? Let's begin!

1.2 How to Create a New Document in Illustrator

Watch Video Lesson (1 min) ↗ 

Let's begin our exploration of Adobe Illustrator by creating a new document. Follow along with this lesson to get started in the software. Keep in mind that you could change these values to suit your project.

illustrator logo designillustrator logo designillustrator logo design

Need a little help with some Adobe Illustrator basics? Consider bookmarking or checking out these beginner Illustrator tutorials for extra help:

1.3 How to Make Shapes and Lines in Adobe Illustrator

Watch Video Lesson (6 mins) ↗ 

Lines and shapes are some of the core aspects of creating artwork within Adobe Illustrator. In this lesson, you'll get a feel for how to use them yourself. We'll start simple, so we can build upon these shapes later.

You can find the Shape Tools and the Line Tool within the Tools panel.
Illustrator Shape ToolsIllustrator Shape ToolsIllustrator Shape Tools
Can't find a specific Shape Tool? Click and hold to expand the tools in the Tools panel. For example, your Ellipse Tool may be nested under your Rectangle Tool.

1.4 How to Use the Pen Tool in Illustrator

Watch Video Lesson (5 mins) ↗ 

The Pen Tool is another Adobe Illustrator essential. In this lesson, you'll learn how the Pen Tool works. It's a great way to create custom shapes and curves. You can find the Pen Tool in the Tools panel.

Don't worry if you've never used it before—this lesson will give you all the ins and outs you need to get started. We'll create points and customize them too. 

Illustrator Pen ToolIllustrator Pen ToolIllustrator Pen Tool

Want to see more of the Pen Tool in action? You can learn even more about working with the Pen Tool in this free tutorial:

1.5 How to Use the Selection Tools in Illustrator

Watch Video Lesson (4 mins) ↗ 

There are two Selection Tools you should know in Adobe Illustrator: The Selection Tool and the Direct Selection Tool. In this lesson, you'll learn the difference between the two and see them in action.

Selection Tool

Use the Selection Tool to select artwork in your workspace, overall.

For example, you could select an entire shape with the Selection Tool.

Direct Selection Tool

Use the Direct Selection Tool to select points in your artwork.

For example, you could select one point in a shape with the Direct Selection Tool.

Not sure which selection tool to use? Remember to keep an eye on the selection you've made in Illustrator (what it has highlighted). Consider the Selection Tool your "main" selection tool.

1.6 How to Use the Stroke Panel in Adobe Illustrator

Watch Video Lesson (6 mins) ↗ 

We'll use the Stroke panel in this lesson to customize our strokes—from the size to the color. You can open the Stroke panel by going to Window > Stroke. Ready to give it a try?

Illustrator Stroke PanelIllustrator Stroke PanelIllustrator Stroke Panel
You can change your Stroke and Fill colors from the Tools panel. This is also a quick place where you can turn the Stroke and Fill colors "off" if you don't want them visible.

2. How to Work With Shapes in Illustrator

2.1 How to Use the Layers Panel in Adobe Illustrator

Watch Video Lesson (2 mins) ↗ 

Layers are a powerful part of Adobe Illustrator. They're going to help us take our logo design even further. Before we dig into using this feature for creative logo design, let's take some time to explore how layers work—and why you'd want to use them.

Open your Layers panel by going to Window > Layers. Keep in mind that you can create, delete, duplicate, and hide layers from this panel.

Illustrator LayersIllustrator LayersIllustrator Layers

Want to learn even more about the ins and outs of Adobe Illustrator layers? Check out these free tutorials:

2.2 How to Adjust Transparency in Adobe Illustrator

Watch Video Lesson (4 mins) ↗ 

Sometimes, you may want to experiment with transparency as you explore creative logo design. In this lesson, we'll take a look at some ways we can use transparency to create some interesting effects in Adobe Illustrator. 

Illustrator TransparencyIllustrator TransparencyIllustrator Transparency
Two shapes with no Transparency applied.
Illustrator TransparencyIllustrator TransparencyIllustrator Transparency
Lowered Transparency applied to the front shape.

2.3 How to Use the Path Finder Tool and Shape Builder Tool

Watch Video Lesson (8 mins) ↗ 

This lesson is all about using our tools to build and manipulate more complex shapes. 

The Path Finder Tool is used to manipulate shapes. For example, you can unite two selected shapes into one. This tool allows us to quickly and simply combine and subtract different parts of our work. 

Illustrator Path Finder ToolIllustrator Path Finder ToolIllustrator Path Finder Tool

Similarly, the Shape Builder Tool also allows us to manipulate shapes—but in some regards, makes it even easier! In this lesson, we'll also walk through how to use this tool to combine and remove parts of compounded shapes.

Illustrator ShapesIllustrator ShapesIllustrator Shapes
Shapes used to create a cloud shape
Illustrator Shape Builder ToolIllustrator Shape Builder ToolIllustrator Shape Builder Tool
Shapes combined with the Shape Builder Tool

You can learn even more about working with the Shape Builder Tool in this free tutorial, right here at Envato Tuts+:

These concepts can be utilized in many different ways. You could, for example, draw an abstract shape with the Pen Tool or achieve a similar result by using the Shape Builder Tool—your workflow, your preferred way.

2.4 How to Use Swatches in Adobe Illustrator

Watch Video Lesson (6 mins) ↗ 

Swatches in Adobe Illustrator are one of the best ways to use, apply, and keep your colors at hand. We'll look at using the Swatches panel in this lesson. This is an important one that we'll use later in the course, as well. Go to Window > Swatches to open up the Swatches panel.

Illustrator SwatchesIllustrator SwatchesIllustrator Swatches
We'll look at Global Swatches in this lesson too—which make changing the colors in your composition even easier!

You can learn even more about working with Swatches in this free Adobe Illustrator tutorial:

2.5 How to Use Gradients in Illustrator

Watch Video Lesson (4 mins) ↗ 

Gradients, when used properly, can help bring depth to our work. That's just what we'll do, once we're ready to explore how to design a logo in Illustrator. For now, we'll start off this lesson by exploring how gradients work. Then, we'll expand upon this with some great ways to use them later in this course.

Illustrator GradientsIllustrator GradientsIllustrator Gradients
You can always go back and adjust your gradients from the Gradient panel. Go to Window > Gradient to open it up! 

Want to learn even more about working with gradients in Adobe Illustrator? Check out this tutorial; it's got everything you need!

2.6 How to Use the Shaper Tool and the Smooth Tool 

Watch Video Lesson (3 mins) ↗ 

Sometimes, you might want to go back and adjust the curve of some of your lines. Thankfully, the Smooth Tool makes doing so a breeze. The Shaper Tool allows us to quickly draw shapes and interprets them for us. Join me in this lesson, where we walk through how to use these tools to make this process simple for us in our logo design process.

Illustrator Smooth ToolIllustrator Smooth ToolIllustrator Smooth Tool
These tools are located in the Tools panel—many of the core tools in Adobe Illustrator are. If you ever have trouble finding a tool, try clicking the "..." symbol at the bottom of this panel. It will help reveal hidden tools.

2.7 How to Use the Knife Tool in Adobe Illustrator

Watch Video Lesson (1 min) ↗ 

Wouldn't it be convenient if we could just slice off part of a shape in our work? Well, with the Knife Tool, we can do just that—and yes, it's as handy as it sounds. Join me in this lesson, where we give the Knife Tool a demonstration in Adobe Illustrator. You can find the Knife Tool in the Tools panel.

Illustrator Knife ToolIllustrator Knife ToolIllustrator Knife Tool
Slices made with the Knife Tool
Knife Tool IllustratorKnife Tool IllustratorKnife Tool Illustrator
Now these slices can be manipulated independently

2.6 How to Use the Scissor Tool in Adobe Illustrator

Watch Video Lesson (1 min) ↗ 

The Scissor Tool is a little similar to the Knife Tool—but instead, we can use it to make different slices in our work. It's a perfect go-to if you want to make a slice at specific points in our work. Check out this lesson's demonstration to get a feel for how this tool works. 

Illustrator Scissor ToolIllustrator Scissor ToolIllustrator Scissor Tool
Making a cut in a Stroke with the Scissor Tool
Scissor Tool IllustratorScissor Tool IllustratorScissor Tool Illustrator
We can then manipulate (or delete) the section we've cut

3. Adobe Illustrator Q&A Session

3.1 Adobe Illustrator Live Stream Q&A Session

Watch Video Lesson (1 min) ↗ 

At this point in the original live stream, we took a break for questions from the audience. While the stream isn't live, you can still tune in and check out this session—and all of the great tips and tricks addressed within it!

If you'd like to skip over the Q&A Session from this live stream, jump to Section 4: How to Create a Logo Design, where we resume the main part of the course content.

3.2 Is There an Automated Way to Separate the Line From the Fill in Illustrator?

Watch Video Lesson (2 mins) ↗ 

Tune in to this lesson to figure out how to separate the stroke from the fill in Adobe Illustrator. There isn't necessarily a completely automated way—but there are certainly simple ways to achieve this. Check out this walkthrough to figure out how.

Illustrator Stoke and FillIllustrator Stoke and FillIllustrator Stoke and Fill

3.3 How  to Zoom In and Out in Adobe Illustrator

Watch Video Lesson (2 min) ↗ 

Being able to navigate your workspace certainly makes working in Adobe Illustrator easier. In this brief Q&A lesson, we'll take a look at how to zoom and pan in Illustrator. It's quick and easy but very useful.

Looking for a shortcut? Zoom in with Control + on PC or Command + on Mac. Zoom out with Control - on PC or Command - on Mac.

3.4 Do the Merging Tools (Shape Builder) Exist in Illustrator CS6 As Well?

Watch Video Lesson (2 mins) ↗

Older versions of Illustrator don't necessarily have the same tools as the current version of Illustrator CC. However, that's not to say that building shapes is impossible in Illustrator CS6. In this Q&A lesson, let's walk through what's available to you if you're running an older version of the software.

3.5 What's the Easiest Way to Make Shapes Striped?

Watch Video Lesson (6 mins) ↗ 

Stripes can be a fun addition to your shapes—and there are multiple ways to achieve this look! Let's explore a couple of ways we could achieve striped shapes in Adobe Illustrator, together, in this Q&A lesson.

Illustrator StripesIllustrator StripesIllustrator Stripes

Want to Learn More About Adobe Illustrator?

Adobe Illustrator for Beginners is a perfect course for those just getting started with Illustrator. Even better, the course is completely free!

Illustrator is an excellent choice for logo design software. Start learning more today!

4. How to Create a Logo Design

4.1 Building the Shape of Our Logo Design

Watch Video Lesson (8 mins) ↗ 

Now that we've taken an in-depth tour of Adobe Illustrator, let's take what we've learned and put it into action. We'll begin building the overall shape of our logo design. 

Take a look at the tools we explored in action, below. There are many ways to achieve these results based on what we learned. Let's walk through some ways to achieve this together in this lesson.

Illustrator StrokeIllustrator StrokeIllustrator Stroke
Illustrator Increase StrokeIllustrator Increase StrokeIllustrator Increase Stroke
Illustrator Direct Selection ToolIllustrator Direct Selection ToolIllustrator Direct Selection Tool

4.2 Creating the Shadows

Watch Video Lesson (6 mins) ↗ 

Let's add some shadows to our work, to bring some depth to our logo design. We'll do so using gradients, as we explored earlier in the course. We'll make them blend in, and manipulate the shape so it seamlessly works with the shape we've put together.

Ready to make some magic happen? Let's begin.

Illustrator ShadowsIllustrator ShadowsIllustrator Shadows

4.3 Adding Highlights

Watch Video Lesson (2 mins) ↗ 

We can also use these concepts to add highlights. Let's take the same idea as the last lesson, and apply it to the top of our logo design. 

Illustrator HighlightsIllustrator HighlightsIllustrator Highlights
Remember, you can always return to your Gradient panel and Swatches panel to make adjustments to the colors in your work. We'll walk through that again in this lesson.

4.4 Adjusting the Logo's Color

Watch Video Lesson (1 min) ↗ 

But what if you want to experiment with your logo design's color? It's time to go back to our swatches again. This time, you'll really get a hands-on feel for just how convenient working with swatches can be. We can easily sample new colors and make adjustments. Ready to give it a try?

Illustrator Global SwatchesIllustrator Global SwatchesIllustrator Global Swatches
This is such a great example of why those swatches from earlier in the course are important! Remember, you can open the Swatches panel by going to Window > Swatches.

4.5 Changing the Color Mode (RBG and CMYK)

Watch Video Lesson (1 min) ↗ 

Your color mode will matter, depending on how you plan to use your work. If you plan to print, for example, you'll likely want to switch over to CMYK. Let's walk through changing the color mode together in this lesson.

RGB stands for "Red Green Blue" and is color intended for screens. CMYK stands for "Cyan Magenta Yellow Black" and is intended for print.

5. How to Complete Your Logo Design in Adobe Illustrator

Watch Video Lesson (1 min) ↗ 

You'll likely need to move or resize your logo at some point. It can be very convenient to group your completed work so that nothing gets accidentally bumped or changed. In this lesson, we'll walk through this simple process together.

You can also ungroup your work by going to Object > Ungroup.

5.2 Using and Positioning Your Work With Artboards

Watch Video Lesson (2 mins) ↗ 

Let's create and adjust our artboards in this lesson. What are artboards? Consider them like a canvas in your work area. You can have more than one, and this might be advantageous in some cases. We'll use the Artboard Tool to adjust, resize, and create some new artboards in this lesson, as we finalize our work.

Illustrator ArtboardsIllustrator ArtboardsIllustrator Artboards
Two artboards at different sizes.

Watch Video Lesson (4 min) ↗ 

Once your logo design is complete, it's time to Export! Let's walk through the process together so you can save and prepare your work for use. We're almost at the finish line!

Illustrator ExportIllustrator ExportIllustrator Export
Because we've made more than one artboard, we have the option of selecting which artboard we'd like to export—you can name them, too, for your convenience!

5.4 Conclusion

Watch Video Lesson (1 min) ↗

You've made it to the finish line and created a logo design, step by step! Congratulations! Now that you've learned how to design your own logo, what will you create? Adobe Illustrator logo design can be a fun, rewarding skill to add to your skillset.

Let's wrap things up together with some final thoughts on Adobe Illustrator and creative logo design. 

Remember, if you need extra help, there's an entire library of professional content on Envato Elements—including logo design templates! 

Learn More About Adobe Illustrator

Want to learn even more about Adobe Illustrator? It's awesome logo design software, but it can do a lot more too! Check out these free video tutorials from Envato Tuts+. There's no better time than right now to watch and learn even more.