Why isn't my CSS transition animation working? Any suggestions on how to troubleshoot and resolve

I am looking to incorporate a transition animation when the image changes, but it seems that the transition is not working as intended. Can anyone provide guidance on how to make the transition style work correctly in this scenario? (Ideally, I would like a left to right animation similar to an image strip rather than ease animation)

Here are the JS, CSS, and HTML codes:

var slides = document.querySelectorAll('.slide');
var next = document.getElementById('next');
var back = document.getElementById('back');
let currentSlide = 0;

var manualNav = function(manual) {
  slides.forEach((slide) => {
    slide.classList.remove('active');
  });
  slides[manual].classList.add('active');
}
next.addEventListener('click', function() {
  if (currentSlide != slides.length - 1) {
    currentSlide++;
    manualNav(currentSlide);
  }
})
back.addEventListener('click', function() {
  if (currentSlide != 0) {
    currentSlide--;
    manualNav(currentSlide);
  }
})
var repeat = function(activeClass) {
  let active = document.getElementsByClassName('active');
  let i = 1;
  var repeater = () => {
    setTimeout(function() {
      [...active].forEach((activeSlide) => {
        activeSlide.classList.remove('active');
      });
      slides[i].classList.add('active');
      i++;
      if (slides.length == i) {
        i = 0;
      }
      if (i >= slides.length) {
        return;
      }
      repeater();
    }, 10000);
  }
  repeater();
}
repeat();
.gallery {
  width: 50%;
  height: 340px;
  display: flex;
  align-items: center;
  flex-direction: column;
}

.gallery .content {
  position: relative;
  width: 564px;
  height: 297px;
}

.gallery .content .slide {
  display: none;
  transition: all 1s ease-in;
}

.gallery .content .slide img {
  width: 100%;
  height: 297px;
  object-fit: cover;
}

.gallery .content .slide.active {
  display: block;
}

.gallery .content .firstSvg {
  position: absolute;
  right: 80px;
  bottom: 24px;
  cursor: pointer;
  height: 50px;
  width: 50px;
  background-color: orange;
}

.gallery .content .lastSvg {
  cursor: pointer;
  position: absolute;
  right: 24px;
  bottom: 24px;
  height: 50px;
  width: 50px;
  background-color: wheat;
}
<div class="gallery" id="gallery">
  <div class="content">
    <div class="slide active">
      <img src="https://i.picsum.photos/id/250/536/354.jpg?hmac=qb3khzryKWU1ECPob2_1mYZLumW5eJTLSmhJzi1VVSI" alt="gallery">
    </div>
    <div class="slide">
      <img src="https://i.picsum.photos/id/868/536/354.jpg?hmac=ZcbB7ABIgl6LS5B1wxkzJ_dxJFgNmCsODUTfxM5GdRk" alt="gallery">
    </div>
    <div class="slide">
      <img src="https://i.picsum.photos/id/441/536/354.jpg?hmac=qHaUqO3vqlz-C811TXJPtRw-FV4ciRCazlDZb1gdodg" alt="gallery">
    </div>
    <div class="firstSvg" id="back">back</div>
    <div class="lastSvg" id="next">next</div>
  </div>
</div>

Answer №1

var slides = document.querySelectorAll('.slide');
var next = document.getElementById('next');
var back = document.getElementById('back');
let currentSlideIndex = 0;

var manualNavigation = function(index) {
  slides.forEach((slide) => {
    slide.classList.remove('active');
  });
  slides[index].classList.add('active');
}
next.addEventListener('click', function() {
  if (currentSlideIndex != slides.length - 1) {
    currentSlideIndex++;
    manualNavigation(currentSlideIndex);
  }
})
back.addEventListener('click', function() {
  if (currentSlideIndex != 0) {
    currentSlideIndex--;
    manualNavigation(currentSlideIndex);
  }
})
var repeatAnimation = function(activeClass) {
  let active = document.getElementsByClassName('active');
  let i = 1;
  var repeater = () => {
    setTimeout(function() {
      [...active].forEach((activeSlide) => {
        activeSlide.classList.remove('active');
      });
      slides[i].classList.add('active');
      i++;
      if (slides.length == i) {
        i = 0;
      }
      if (i >= slides.length) {
        return;
      }
      repeater();
    }, 10000);
  }
  repeater();
}
repeatAnimation();
.gallery {
  width: 50%;
  height: 340px;
  display: flex;
  align-items: center;
  flex-direction: column;
}

.gallery .content {
  position: relative;
  width: 564px;
  height: 297px;
}

.gallery .content .slide {
  height: 0;
  width: 0;
  opacity: 0;
  transition: all 1s ease-in;
}

.gallery .content .slide img {
  width: 100%;
  height: 297px;
  object-fit: cover;
}

.gallery .content .slide.active {
  height: 100%;
  width: 100%;
  opacity: 1;
}

.gallery .content .firstSvg {
  position: absolute;
  right: 80px;
  bottom: 24px;
  cursor: pointer;
  height: 50px;
  width: 50px;
  background-color: orange;
}

.gallery .content .lastSvg {
  cursor: pointer;
  position: absolute;
  right: 24px;
  bottom: 24px;
  height: 50px;
  width: 50px;
  background-color: wheat;
}
<div class="gallery" id="gallery">
  <div class="content">
    <div class="slide active">
      <img src="https://i.picsum.photos/id/250/536/354.jpg?hmac=qb3khzryKWU1ECPob2_1mYZLumW5eJTLSmhJzi1VVSI" alt="gallery">
    </div>
    <div class="slide">
      <img src="https://i.picsum.photos/id/868/536/354.jpg?hmac=ZcbB7ABIgl6LS5B1wxkzJ_dxJFgNmCsODUTfxM5GdRk" alt="gallery">
    </div>
    <div class="slide">
      <img src="https://i.picsum.photos/id/441/536/354.jpg?hmac=qHaUqO3vqlz-C811TXJPtRw-FV4ciRCazlDZb1gdodg" alt="gallery">
    </div>
    <div class="firstSvg" id="back">back</div>
    <div class="lastSvg" id="next">next</div>
  </div>
</div>

Transitions do not apply when using the display property

If you switch from display: none; to display:block for an active slide, the browser won't recognize this as a transition event and no animation will occur.

To create a fade in animation, make the following changes:

Changes

   .gallery .content .slide {
      height: 0;
      width: 0;
      opacity: 0;
      transition: all 1s ease-in;
   }

   .gallery .content .slide.active {
      height: 100%;
      width: 100%;
      opacity: 1;
   }

The slides initially have zero values for height,width,opacity making them 'invisible'.

When a slide is set to active, these properties change triggering the animation specified by transition: all 1s ease-in;

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

Some angles in three.js may cause faces to be obscured

When I work in Blender, my process usually involves creating straightforward models with colored materials. However, when I export these models to Collada format and import them into Three.js, I encounter an issue where some faces do not appear from cert ...

Is there a way to eliminate a CSS class from a div element within the outcome of an ajax response?

My ajax response is functioning properly and displaying the content of the result, but I am always getting the CSS effects which I do not want. I need to eliminate the class="container body-content" from the following div in the ajax call's result. ...

What are the techniques for integrating PHP code into JavaScript/Ajax?

I am curious about how to integrate PHP code into JavaScript/Ajax. Here is the PHP code I am working with: if ($folder = opendir('data/Tasklist/')) { while (false !== ($file = readdir($folder))) { if ($file != '.' && $ ...

Struggling with aligning and floating links to the right while crafting a custom navigation bar

I'm in the process of crafting a custom navigation bar for my website, with the intention of having my name prominently displayed on the far left while the links float to the far right. I've utilized CSS to style everything within the nav section ...

Having trouble with React JS BrowserRouter not routing correctly when used with Express JS and Nginx proxy

I am facing an issue where I need to send parameters to a React component through the URL. This works perfectly fine when I test it in my localhost development environment. However, the problem arises when I deploy the React app to my live server (). The ...

Transmit information from a JavaScript AJAX function to a JSP page

My current goal involves a user clicking on a link on the home page, let's say /home.jsp. Upon clicking this link, I extract the value and use it to call a RESTful resource that interacts with a database and returns a response. The communication with ...

What is the process for retrieving a value from JSON utilizing JavaScript?

Unfortunately, I am completely stumped when it comes to Javascript. I'm attempting a few solutions based on online resources. If anyone could offer some assistance, that would be greatly appreciated. Below is the JSON data provided. It has been conde ...

What is the best way to show the current value of a checkbox element in the future?

I want to add multiple checkboxes using this method: $(".btn-sample").on("click", function() { $(".container").append( '<input class="check-sample" type="checkbox" value="'+check_value+'"/>' ); }); The issue I' ...

assigning a numerical value to a variable

Is there a way to create a function for a text box that only allows users to input numbers? I want an alert message to pop up if someone enters anything other than a number. The alert should say "must add a number" or something similar. And the catch is, w ...

Replace jQuery CSS with standard CSS without using !important to override styles

Recently, I encountered a puzzling situation: I have an element with the following CSS properties ($(this) represents the cloned object): clone.css({ 'width': $(this).width() + 'px', 'height': $(this).height() + ' ...

Helping individuals identify the HTML5 Geolocation notification

Currently working on a website that requires users to accept the browser prompt for location sharing. Many users seem to overlook this prompt, which can lead to issues. The main problem we are facing is that each browser displays this prompt differently: ...

What is the method for adjusting the text color in an HTML document?

Is there a way to modify the text color of this statement: You Have Already Earned Credit For This Link? <div id=\"myform\" style=\"visibility: hidden;\"> <font size=2><b>You Have Already Earned ...

Using PHP to create a mailto link with a variable that includes a FontAwesome icon

Hi! I'm trying to get this code to check if the ACF variable is empty, and if not, print out HTML code for a globe icon with a mailto link called 'mail'. However, it doesn't seem to be working. Any thoughts on what might be wrong? &l ...

How to hide text within a contenteditable element

I'm trying to create a contenteditable span that behaves like a password field in a Linux terminal; the text is entered, but not visible. I've experimented with different CSS properties like visibility: hidden and display: none, but they don&apos ...

Creating a column for dates using the js-xlsx library

After multiple attempts using js-xlsx, I have encountered an issue when trying to write a XLSX file with a Date column. Whenever I open the file in Excel 2010, the date is displayed as the number of days from a specific origin rather than in the desired fo ...

Tips for effectively implementing correct reselection in a React-Redux application

I have a system that allows users to search for data based on their input. I am currently implementing reselect in my application. import React, { useEffect } from "react"; import { useDispatch, useSelector } from "react-redux" ...

I'm interested in knowing how to switch between two functions using Angular

I have developed a grocery list application where each row contains the name of a food item. The layout includes a left column for items that are not needed and a right column for items that are needed. Currently, I am able to change the state by clicking ...

Showing JSON data in AngularJS

I need help with AngularJS to display JSON output. Is using gridOptions the best approach for this? I'm trying to print labels starting from the root in reverse order - label/parent's label/parent's parent's label/... { artifac ...

Change the position of the specified footer on the one-page website

I am currently in the process of creating a single-page layout website. What I want is to have the elements with the class "footer-bar" positioned absolutely on each page, but on the #Home Page, it should be position relatively. Does anyone have any sugge ...

Remove data from MySQL using a JavaScript function

I have a database with a list of items that I want users to be able to delete by clicking on a link. I need this to happen without refreshing the page. Although I am not very experienced with JavaScript, I am trying my best. This is how I currently have i ...