Creating Daily Loading Animations in Webflow with IX2 Interactions and Cookies: A Beginner's Guide

Published on
May 13, 2019

A Beginner's Guide to Creating Loading Animations in Webflow Using IX2 Interactions and Cookies

Are you looking to add a loading animation to your website that only plays once per day? Well, with the magic of Webflow and a little bit of JavaScript, you can create this impressive feature. In this guide, we'll walk through the process of using IX2 interactions and cookies to achieve this unique functionality.

Understanding the Concept

Before we dive into the technical aspects, let's take a moment to understand the intended outcome. The goal is to display a loading animation only once per day for users visiting the website. This ensures that returning visitors do not encounter the loading animation every time they access the site within a single day. Achieving this functionality requires a combination of Webflow's IX2 interactions and JavaScript to manipulate cookies.

Setting Up the Webflow Project

To begin, open your Webflow project and ensure you have a clear understanding of the layout and structure of your website. For the purpose of this tutorial, we'll assume you already have a basic understanding of Webflow's interface and design principles.

Designing the Loading Animation

Start by creating the loading animation itself. This can be achieved by adding a loading wrapper above the main content of your website. In the Webflow Designer, you can place a div block or any other element at the top of your page and designate it as the loading wrapper. Give it a class name, for example, "loading-wrapper," and set it to display:none.

Configuring IX2 Interactions

In Webflow's Interactions panel, set up a page load interaction for the entire page. Target the "loading-wrapper" element and create a sequence of actions within the page load interaction. These actions should include setting the loading wrapper to a display property of flex, followed by adjusting its opacity, and finally setting it back to display:none.

This simple interaction will be triggered when the page loads, but we will enhance this functionality with JavaScript and cookies to achieve our desired outcome.

Implementing the Loading Animation Logic

Now that we have set up the foundation in Webflow, let's dive into the JavaScript that will control the behavior of the loading animation based on past site activity.

Adding JavaScript Code

In the Head section of your Webflow project, add a custom code block and input the following JavaScript code:

// Set initial style for the page wrapper to prevent flickeringdocument.querySelector('.page-wrapper').style.display = 'none';// Define the page wrapper and loading wrapper elementsconst pageWrapper = document.querySelector('.page-wrapper');const loadingWrapper = document.querySelector('.loading-wrapper');// Define the name of the cookie for the loading animationconst animationCookie = 'seen-animation';// Check if the cookie is already setif (document.cookie.indexOf(animationCookie) === -1) {  // Show the loading animation  loadingWrapper.style.display = 'flex';  // Set a timeout to hide the loading animation after a specific duration  setTimeout(() => {    pageWrapper.style.display = 'block';  }, 3000); // Adjust the duration based on the length of your loading animation  // Set the cookie to indicate that the animation has been seen  document.cookie = `${animationCookie}=1; expires=${new Date(new Date().getTime() + 86400000).toUTCString()}; path=/`;} else {  // Hide the loading animation and display the page content  loadingWrapper.style.display = 'none';  pageWrapper.style.display = 'block';}

Let's break down the key components of the JavaScript code piece by piece:

  1. The initial style for the page wrapper is set to "display:none" to prevent any flickering of content on page load.
  2. References to the page wrapper and loading wrapper elements are established using querySelector.
  3. A variable "animationCookie" is defined to hold the name of the cookie that tracks the viewing of the animation.
  4. An if-else statement checks for the presence of the "seen-animation" cookie. If the cookie is not found, the loading animation is displayed, and the cookie is set with an expiration time of 24 hours. If the cookie is present, the loading animation is hidden, and the page content is displayed.

By using this JavaScript code in combination with Webflow's IX2 interactions, you can achieve the desired functionality of showing the loading animation once per day for new visitors while providing a seamless experience for returning users.

Conclusion

In this tutorial, we explored the process of incorporating a loading animation that plays only once per day using Webflow's IX2 interactions and JavaScript cookie handling. By combining the visual capabilities of Webflow with the logic of JavaScript, you can create a polished user experience that balances engaging animations with efficient navigation.

As you continue to explore Webflow's capabilities, consider implementing similar techniques to enhance the user experience on your websites. With the power of Webflow and a touch of custom scripting, you can build interactive and dynamic web experiences that captivate your audience. We hope this guide has inspired you to experiment with new features and functionalities in your Webflow projects.