jQuery animation that smoothly fades out from the left and fades in from the right

This survey is designed to assist individuals in determining where they should go with a specific type of ticket.

Currently, everything is functioning smoothly, but I would like to add a sleek animation to each ul transition.

Whenever a button is clicked, the old ul will fade-out to the left while the new ul fades-in from the right. If the Back button is pressed, the opposite animation will occur when returning to the previous ul.

At the moment, I am utilizing animations from . Is there a way to control these animations using jQuery, or would it be more effective to create the animations solely with jQuery?

$(document).ready(function() {
  $(".tab a").click(function() {
    $(this).parent().addClass("active").siblings(".active").removeClass("active");
    var tabContents = $(this).attr("href");
    $(tabContents).addClass("active").siblings(".active").removeClass("active");
    return false;

    setTimeout(function() {
      q_list.eq(num).addClass("left");
      setTimeout(function() {
        q_list.eq(num).hide();
        q_list.eq(num + 1).show();
        btn.removeClass("click");
        setTimeout(function() {
          q_list.eq(num + 1).removeClass("right");
          num++
        }, 1000);
      }, 360);
    }, 260);
  });

  $("button").on("click", function() {
    let currentLevel = $(this).closest('.tab').attr('data-level');
    let prevLevel = parseInt(currentLevel) - 1;
    $('.tab').removeClass("active");
    $(`.tab[data-level=${prevLevel}]`).eq(0).addClass('active')
  });
});
.question_wrapper {
  display: flex;
  justify-content: center;
}

ul {
  list-style: none;
  border: 1px solid black;
  text-align: center;
  padding-left: 0;
  width: 40rem;
  height: 20rem;
  display: flex;
  justify-content: center;
  align-items: center;
}

.buttons {
  display: flex;
  justify-content: space-around;
}

.yes_part,
.no_part {
  display: flex;
  flex-direction: column;
}

.yes_part {
  padding-right: 2rem;
}

a.yes {
  background-color: #FFB8B8;
  padding: 5px 20px;
  text-decoration: none;
  color: black;
  margin-top: 5px;
}

a.no {
  background-color: #B1E0FF;
  padding: 5px 20px;
  text-decoration: none;
  color: black;
  margin-top: 5px;
}

.ticket_type {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  padding-bottom: 10px;
}

.ticket_type a {
  padding: 10px 30px;
  border: 1px solid black;
  border-radius: 10px;
  margin: 10px;
}

.answer_1 {
  display: flex;
  flex-direction: column;
}

.store_place_1,
.store_place_2 {
  display: flex;
}

.store_1,
.store_2,
.store_3,
.store_4,
.store_5 {
  text-decoration: none;
  color: black;
}

a {
  text-decoration: none;
  color: black;
}

.tab {
  display: none;
}

.tab.active {
  display: flex;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />

<section class="question_wrapper">
  <ul class="tab main-tab active animate__animated animate__fadeInRight" data-level='1'>
    <li class="active">
      <p>Do you have a ticket ?</p>
      <div class="buttons">
        <div class="yes_part">
          <img src="yes.png" alt="">
          <a class="yes" href="#tab1">yes</a>
        </div>
        <div class="no_part">
          <img src="no.png" alt="">
          <a class="no" href="#tab2">no</a>
        </div>
      </div>
    </li>
  </ul>
  <ul class="tab animate__animated animate__fadeInRight" id="tab1" data-level='2'>
    <li>
      <p>Which Ticket do you have ?</p>
      <div class="ticket_type">
        <a href="#tab3">Type 1</a>
        <a href="#tab3">Type 2</a>
        <a href="#tab3">Type 3</a>
        <a href="#tab4">Type 4</a>
        <a href="#tab4">Type 5</a>
        <a href="#tab5">Type 6</a>
      </div>
      <button type="button">Back</button>
    </li>
  </ul>
  <ul class="tab animate__animated animate__fadeInRight" id="tab2" data-level='2'>
    <li>
      <p>You can buy the ticket from here.</p>
      <div class="store_place_1">
        <a href="#">
          <div class="store_1">
            <p>Store 1</p>
            <img src="" alt="">
            <p>You need to login first to buy</p>
          </div>
        </a>
        <a href="#">
          <div class="store_2">
            <p>Store 2</p>
            <img src="" alt="">
            <p>You need to login first to buy</p>
          </div>
        </a>
        <a href="#">
          <div class="store_3">
            <p>Store 3</p>
            <img src="" alt="">
            <p>You need to login first to buy</p>
          </div>
        </a>
      </div>
      <div class="store_place_2">
        <a href="#">
          <div class="store_4">
            <p>Store 4</p>
            <img src="" alt="">
            <p>You need to login first to buy</p>
          </div>
        </a>
        <a href="#">
          <div class="store_5">
            <p>Store 5</p>
            <img src="" alt="">
            <p>You need to login first to buy</p>
          </div>
        </a>
      </div>
      <button>Back</button>
    </li>
  </ul>
  <ul class="tab animate__animated animate__fadeInRight" id="tab3" data-level='3'>
    <li>
      <div class="answer_1">
        <p>Please go to hall A</p>
        <img src="receptionist.png" alt="">
        <button>Back</button>
      </div>
    </li>
  </ul>
  <ul class="tab animate__animated animate__fadeInRight" id="tab4" data-level='3'>
    <li>
      <p>Please go to hall B</p>
      <button>Back</button>
    </li>
  </ul>
  <ul class="tab animate__animated animate__fadeInRight" id="tab5" data-level='3'>
    <li>
      <p>Please go to hall C</p>
      <button>Back</button>
    </li>
  </ul>
</section>

Answer №1

In this implementation, Animate.css is used to create a customizable set of transitions for elements on a webpage. By adjusting the trans Object, you can specify different transitions for { Current->in, Current->out, Next->in and Next->out }. Additionally, you can control the speed with options like fast or '' (default).

  • On page initialization, default transitions are applied to all animated elements (default is the trans.nextIn class)
  • On page initialization, the speed configuration class is applied to all animated elements
  • During a 'forward' transition (triggered by a click), the current element receives the nextOut transition and a timeout is set. Once the timeout completes, the target tab is given the 'active' + nextIn transition class
  • During a 'back' transition, the reverse process occurs.

The code includes comments to explain each section for better understanding.

let trans = {
  nextIn: 'animate__fadeInLeft',
  nextOut: 'animate__fadeOutRight',
  prevIn: 'animate__fadeInRight',
  prevOut: 'animate__fadeOutLeft'
};
let transArr = Object.entries(trans).map(e => e[1]);
let aDelay, speed = 'fast'; // Options: fast or slow

$(document).ready(function() {
  // Setup animations based on configuration
  $('.animate__animated').addClass(trans.nextIn);  // Apply default IN transition
  if (speed === 'fast') {
    aDelay = 200;
    $('.animate__animated').addClass('animate__faster');
  } else {
    aDelay = 300;
  }

  $(".tab a").click(function() {
    $('.tab').removeClass(transArr);  // Remove all transition classes from tabs
    $(`.tab.active`).addClass(trans.nextOut);
    var tabContents = $(this).attr("href");
    $(tabContents).removeClass(transArr);
    setTimeout(() => {
      $(`.tab.active`).removeClass('active');
      $(tabContents).addClass([trans.nextIn, "active"]);
    }, aDelay);
  });

  $("button").on("click", function() {
    let currentLevel = $(this).closest('.tab').attr('data-level');
    let prevLevel = parseInt(currentLevel) - 1;
    $('.tab').removeClass(transArr);  // Remove all transition classes from tabs
    $(`.tab.active`).addClass(trans.prevOut);
    setTimeout(() => {
      $('.tab').removeClass('active');
      let targTab = `.tab[data-level=${prevLevel}]`;
      $(targTab).eq(0).addClass('active ' + trans.prevIn);
    }, aDelay);
  });
});
.question_wrapper {
  display: flex;
  justify-content: center;
}

/* Additional CSS styles for the question wrapper */
... 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
/* HTML structure with interactive tabs using animate.css effects */
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Effectively accessing the Templater object within a user script for an Obsidian QuickAdd plugin

I have developed a user script to be used within a QuickAdd plugin Macro in Obsidian. The Macro consists of a single step where the user script, written in JavaScript following the CommonJS standard, is executed. Within this script, there is a task to gene ...

A guide to building a versatile dropdown component with Material UI or MUI in a React application

Is there a way to create a reusable dropdown component using Material UI or MUI in React? ...

Unexplained quirks observed in AngularJS $watch during the initialization of a controller

I am working with a code snippet that utilizes AngularJS: var app = angular.module('Demo', []); app.controller('DemoCtrl', function ($scope) { function notify(newValue, oldValue) { console.log('%s => %s', oldValue, ...

Show the GitHub repositories of the user within a React application

Even though I am relatively new to React, I managed to create a GitHub search application. In this app, you can input a user's name in a search box and view their avatar, bio, username, etc. However, I'm facing an issue with fetching their reposi ...

Webpack resolve.alias is not properly identified by Typescript

In the Webpack configuration, I have set up the following: usersAlias: path.resolve(__dirname, '../src/pages/users'), In my tsconfig.json, you can find: "baseUrl": ".", "paths": { "usersAlias/*": ["src/pages/users/*"], } This is how the cod ...

What's the best way to display my slider exclusively on the homepage?

Hello, I am experiencing an issue with my slider. I would like to display the slider only on the home page and not on any other sub-pages. Is there a way to achieve this? Below is the code that I am using: <script> $( ...

Is there a way to download a file using an ajax request?

We are faced with a particular scenario in our application where: Client sends a request Server processes the request and generates a file Server sends the file back as a response Client's browser prompts a dialog for downloading the file Our appli ...

Looking for a way to choose a button with a specific class name and a distinct "name" attribute using jquery?

I am currently working on developing a comment system. As part of this system, I want to include a toggle replies button when a user posts a reply to a comment. However, I only want this button to be displayed if there are no existing replies to the commen ...

What functionality does the --use-npm flag serve in the create-next-app command?

When starting a new NextJS project using the CLI, there's an option to use --use-npm when running the command npx create-next-app. If you run the command without any arguments (in interactive mode), this choice isn't provided. In the documentati ...

Error: Unexpected data type encountered in JSON parsing - was expecting an array but found a string

I am currently integrating Gson with JQWidgets in my project. In Step 1, I am utilizing the Gson API to send JSON data to the grid in the format specified below for it to display rows supported by JQWidgets. [ { "CompanyName": "Alfreds Futterkiste", "C ...

Typeahead AJAX in Bootstrap 3

I have implemented the Bootstrap 3 Typeahead plugin This is my current code: <input type="text" class="typeahead" autocomplete="off"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script&g ...

Why won't my controller function fire with ng-click in AngularJS?

I'm having trouble getting a function to execute when my button is clicked. Despite the fact that this code appears correct, the function defined in my controller isn't being triggered. The code compiles without errors as shown in the console. A ...

Chrome is experiencing a problem with anchor tags that have an href attribute set to a "blob:" URL and using a target of "_blank"

My current project involves developing a website feature that allows users to download a PDF version of a page. To achieve this, the generated HTML is rendered into a PDF on the server, which is then returned as a Base64-encoded PDF. This data is converted ...

Beyond the footer area on the right side

I have a two-column layout, but I'm having issues with the footer area. When I add a border around it, the right side extends beyond what I expected. Check out the issue here. I tested it in Firefox and used some CSS: footer { border-style: solid ...

Issue with CSS line height - Struggling to determine the precise number of lines

Despite finding an old post on this topic, the most popular answer and other solutions didn't work for many people. Even years later, I am still facing the same issue. My goal is to create a div that is precisely 19 lines tall (ideally aiming for 19. ...

jQuery EasyUI: Automate tree expansion upon page loading

Currently, I am utilizing the jQuery EasyUI framework for a project that involves a tree-based structure. My concern is that I would like the tree to be automatically expanded by default. Below is the code snippet: var $arbol = jQuery("#arbol-jerarquia-h ...

Problem encountered during the transfer of JSON data from PHP to JavaScript

Currently, I am working on a PHP file that extracts data from a database to display it on a chart using the Chart.js library. The chart is functioning properly, but I am facing an issue where I need to use the json_encode() function to pass the array value ...

turning every input field's border to red if none of them were filled out at least once

I struggle with javascript and need some help. I have a form with multiple input fields, and I want to ensure that the user fills in at least one of them. I found code that triggers an alert message if the user does not fill in any fields, but I would pref ...

Tips for enlarging the box width using animation

How can I create an animation that increases the width of the right side of a box from 20px to 80px by 60px when hovered over? What would be the jQuery code for achieving this effect? ...

What is the reason this straightforward regex functions perfectly in all cases, except for when applied to the html5

This particular HTML input turns red to signify that the pattern has not matched when the value in the input field is "1". var newInput = document.createElement ('input'); newInput.pattern = '^\d+\.?\d*$'; document.getEl ...