Is your AOS animation failing to work when you switch tabs?

function openCity(event, cityName) {

  var i, tabContent, tabLinks;
  tabContent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabContent.length; i++) {
    tabContent[i].style.display = "none";
  }
  tabLinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tabLinks.length; i++) {
    tabLinks[i].className = tabLinks[i].className.replace(" active", "");
   }
  document.getElementById(cityName).style.display = "block";
  event.currentTarget.className += " active";
}

document.getElementById("defaultOpen").click();
body {font-family: Arial;}

/* Styling for the tabs */
.tab {
 overflow: hidden;
 border: 1px solid #ccc;
 background-color: #f1f1f1;
 }

/* Button styling for the tabs */
.tab button {
 background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
 font-size: 17px;
}

/* Hover effect for buttons */
.tab button:hover {
  background-color: #ddd;
}

/* Active button styling */
.tab button.active {
  background-color: #ccc;
}

/* Styling for the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
</head>
<body>
<p data-aos="zoom-out-up">This example demonstrates using JavaScript to automatically select the London tab upon page load.</p>

<div class="tab">
 <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
 <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
 <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent" data-aos="zoom-in">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
   </div>

<div id="Paris" class="tabcontent" data-aos="fade-down">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent" data-aos="flip-up">
 <h3>Tokyo</h3>
 <p>Tokyo is the capital of Japan.</p>
</div>
<script src="https://unpkg.com/aos@next/dist/aos.js"></script>
  <script>
    AOS.init();
  </script>
  </body>
</html> 

I am exploring AOS animations and encountered a problem with the code above. The animation functions properly for the first tab content but does not work for the second and third tabs. Additionally, when switching between tabs, the animation fails to trigger. I have not included any extra libraries in my code. Any suggestions on how to resolve this issue would be greatly appreciated.

Answer №1

As elements scroll into view vertically, AOS adds animation effects to them. This is why the first tab animates when the page loads, while other hidden elements remain static even after they become visible. To animate elements only after they are visible, consider using a different library that applies animations by adding specific classes programmatically. One alternative suggestion is Animate.css

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

Modify the background color of the entire page when the main div is clicked

I am attempting to alter the background color of my entire page when the div with id main is clicked. You can view the code here: $(document).ready(function() { var color = 1; $('#main').click(function(){ if (colo ...

What is the method for determining the required or imported base path?

My package.json includes the main option to set the default file for importing like import 'my-index' from 'my-module' However, I would like to break up my-module into separate files so developers can include them individually with sta ...

What is the best way to toggle the visibility of multiple column groups in Ag-Grid community using dynamic

I am seeking to replicate a basic version of the sidebar found in Ag-Grid Enterprise. The goal is to use JavaScript to gather all column groups within a grid and then provide a checkbox for each group to toggle visibility. While I am aware that individual ...

"click on the delete button and then hit the addButton

I have created a website where users can save and delete work hours. I am facing an issue where I can save the hours at the beginning, but once I delete them, I cannot save anything anymore. Additionally, when I reload the page, the deleted data reappears. ...

Storing data locally for a task management application

This is a standard to-do list. You can add or remove items, but the problem is that they disappear when you refresh. I thought about using localStorage to fix this issue. I successfully created an array to store the items. Now, my goal is to display the it ...

Implementing global parameters in ui-router

Currently, I am utilizing ui-router in AngularJS as shown below: .state ('browse.category', { url: "/:category", templateUrl: "views/browseCategory.html", controller: function($stateParams, $scope) { $scope.params = $st ...

What is the best way to invoke a method using innerHTML?

I am struggling with two methods, DisplayBookmark() and deleteBookmark(). The DisplayBookmark method has a URL. When I click the delete button, I need to extract that URL and pass it to the deleteBookmark() method. I want to change this line: <a class= ...

What is the process for enabling HLS.js to retrieve data from the server side?

I have successfully implemented a video player using hls.js, and I have some ts files stored in https:/// along with an m3u8 file. To read the content of the m3u8 file, I used PHP to fetch it and sent the data to JavaScript (res["manifest"] = the content ...

Let's search for the secret rabbit script in the code

Can you try turning this exercise into a "rabbit catching" game where the rabbit moves when you hover over it? After 4 tries, display a message saying "No Easter Eggs". After 20 attempts, show "Humans Suck". Design the page with a single div element with f ...

React is unable to access and retrieve data from a web API

Currently, I have a controller set up in web-api for my project. I am using React v18.2.0 and NextJS v13.3.0 to work with this setup: public List<ReturnDat> Get() { List<ReturnDat> lst = new List<ReturnDat>(); lst.Add(new ReturnD ...

Is there a reason for the absence of the Revit category attribute in the JSON response retrieved from the GET request :urn/metadata/:guid/

After receiving the information from the endpoint regarding Revit Models uploaded to my bucket, I noticed that the JSON response contains multiple objects. These objects seem to represent Revit elements, each with all parameters except for the Revit Categ ...

Exchange content of div on click using AJAX technology

I'm looking to use AJAX to swap the content of two divs when a button is pressed. I created a simple jsfiddle to demonstrate what I mean. Essentially, I want to exchange A with B and B with A simultaneously (while keeping the red and green boxes in p ...

Effortlessly make a ThreeJs object follow the mouse's movement

I'm currently working on a project where I want an object to continuously track the mouse in a more natural manner. Here's what I've accomplished so far: Ensured that the object always looks at the mouse Added easing for a smoother, more n ...

Element positioning in Material UI is fluid and responsive across all devices, with the ability to place elements at

As a novice in layout design, I am currently developing a web application for both mobile and desktop using React and Material UI. My challenge lies in placing the app navigation at the bottom of the screen. The issue arises because the location of the app ...

Updating vertices in a THREE.Points or THREE.ParticleSystem in Three.js: A step-by-step guide

I have come across examples discussing how to set the objects' vertices (such as THREE.Vector3) properties like .velocity and setting the geometry.__dirtyVertices of the particleSystem to true. However, I cannot locate these properties in my current ...

Guide to generating dynamic arrays in JavaScript when a button is clicked

I am working on a JavaScript program where I dynamically generate buttons and div tags, as well as retrieve data from a local JSON file. My goal is to implement a new feature where clicking a button will create an array with the same name as the button, al ...

How to organize elements in groups of three using HTML and PHP

Hey there, I'm currently working on aligning items in rows of 3. Each row should start with a div called "". The main idea is to open a new div after every 3 items and close the previous one. I've tried the code below but unfortunately, it' ...

Execute the script repeatedly

(Since Stack snippet is temporarily down, I included a jsfiddle link): Visit this link for the code I've been struggling to get this script to work. Here's the simple Javascript code I have: $(document).ready(function() { showMessage(); s ...

Prevent clicking on the <div> element for a duration of 200 milliseconds

I need to make this box move up and down, but prevent users from clicking it rapidly multiple times to watch it go up and down too quickly. How can I add a 200 millisecond delay on the click or disable clicking for that duration? View the jsfiddle example ...

Check the checkbox on the javascript console using the label names

I need to use JavaScript to select checkboxes in a specific way. My challenge is checking over 300 checkboxes with unique names. I know how to check one checkbox or all of them, but how do I target specific checkboxes? For example, let's say I have ...