How do I incorporate a smooth transition effect for opacity in this slideshow code? Part Two

Sorry to bring this up again, but I have made some progress since yesterday. However, I am still unable to complete it, and I'm starting to feel a bit desperate.

If you recall, the question I posted yesterday can be viewed here: How can I add opacity transition to this script?

Following Anthony's advice, I am attempting to apply an additional class called .visible to the visible image of "mySlides" and remove it when the next image is displayed.

After some investigation, I was able to assign the new class with the following line of code: x[slideIndex-1].classList.add("visible")

Unfortunately, the transition effect still does not seem to work as intended.

Can someone please help me identify what mistake I might be making in this case?

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i,
    x = document.getElementsByClassName("mySlides"),
    dots = document.getElementsByClassName("demo");

  if (n > x.length) {
    slideIndex = 1
  }

  if (n < 1) {
    slideIndex = x.length
  }

  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
    x[i].classList.remove("visible");
  }

  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" w3-white", "");
  }

  x[slideIndex - 1].style.display = "block";
  x[slideIndex - 1].classList.add("visible")
  dots[slideIndex - 1].className += " w3-white";
}
img.mySlides {
  opacity: 0;
  transition: opacity 2s;
}

img.mySlides.visible {
  opacity: 1;
  transition: opacity 2s
}
<div class="w3-content w3-display-container">
  <div id="slider-wrapper">
    <img id="zupfinstrumente" class="centerscreen mySlides slidepos" src="img1.jpg" alt="xy">
    <img id="ueber-mich" class="centerscreen mySlides slidepos" src="img2.jpg" alt="xy">
    <img id="streichinstrumente" class="centerscreen mySlides slidepos" src="img3.jpg" alt="xy"></div>
  <div class="w3-center w3-container w3-section w3-large w3-text-white centerscreen badgepos">
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
  </div>
  <container id="buttons" class="centerscreen">
    <div id="navarrow-left" class="w3-left w3-hover-text-black btn" onclick="plusDivs(-1)">&#10094;</div>
    <div id="navarrow-right" class="w3-right w3-hover-text-black btn" onclick="plusDivs(1)">&#10095;</div>
  </container>
</div>

Answer №1

You have the option to create a customized CSS animation and apply it to your img. This way, the animation will trigger when the image is displayed.

Here is an example of the animation in CSS:

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

Check out the code snippet below:

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
showDivs(slideIndex += n);
}

function currentDiv(n) {
showDivs(slideIndex = n);
}

function showDivs(n) {
var i,
x = document.getElementsByClassName("mySlides"),
dots = document.getElementsByClassName("demo");

if (n > x.length) {slideIndex = 1
} 

if (n < 1) {slideIndex = x.length
}

for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}

for (i = 0; i < dots.length; i++) {
  dots[i].className = dots[i].className.replace(" w3-white", "");
}
 
x[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " w3-white";
}
.mySlides {display:none}
.w3-left, .w3-right, .w3-badge {cursor:pointer}
.w3-badge {height:13px;width:13px;padding:0}


/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .1} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .1} 
  to {opacity: 1}
}
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"/>
<div class="w3-content w3-display-container">
    <div id="slider-wrapper">
        <img id="zupfinstrumente" class="centerscreen mySlides slidepos fade" src="https://www.w3schools.com/w3css/img_mountains_wide.jpg" alt="xy">
        <img id="ueber-mich" class="centerscreen mySlides slidepos fade" src="https://www.w3schools.com/w3css/img_fjords_wide.jpg" alt="xy">
        <img id="streichinstrumente" class="centerscreen mySlides slidepos fade" src="https://www.w3schools.com/w3css/img_nature_wide.jpg" alt="xy"></div>
    <div class="w3-center w3-container w3-section w3-large w3-text-white centerscreen badgepos">
        <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
        <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
        <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
    </div>
    <container id="buttons" class="centerscreen">
        <div id="navarrow-left" class="w3-left w3-hover-text-black btn" onclick="plusDivs(-1)">&#10094;</div>
        <div id="navarrow-right" class="w3-right w3-hover-text-black btn" onclick="plusDivs(1)">&#10095;</div>
    </container>
</div>

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

Avoiding the collapse of the Bootstrap 4 navbar

Having trouble with the navbar collapsing on itself? The issue seems to occur around 1280px window size, but works fine when the window is wider or narrower. https://www.bootply.com/AcsaaqReAL .navbar-custom { background-color: #333333; border- ...

When working with create-react-app and TypeScript, you may encounter an error stating: "JSX expressions in 'file_name.tsx' must

After setting up a React project with TypeScript using the CLI command create-react-app client --typescript, I encountered a compilation error when running npm start: ./src/App.js Line 26:13: 'React' must be in scope when using JSX react/r ...

The constant increase in page header number leading to minor interruptions during page scrolling

On my website, I have a dynamic page header that shows the number of comments a user has scrolled through. You can see this feature in action here: However, there seems to be a slight jitter on the screen every time the comment counter updates... To disp ...

Unable to resolve the issue with ExpressPeerServer not being recognized as a function in server.js

I'm facing an issue with the peer.js library in my npm project. I have successfully installed it, but when I try to use it in my server.js file, I get an error saying that peerServer is not a function. const express = require('express'); con ...

Modify the height of the panel-header in Bootstrap

I'm attempting to adjust the height of Bootstrap panel-header. However, when I modify the height using the following CSS code: style="height: 20px;" The title inside the header becomes misaligned. To see an example, check out this Plunker demo. Wh ...

Internet Explorer no longer supports SCRIPT tags in AJAX responses

We are currently facing an issue in our project where AJAX returns some code, and we utilize innerHTML to insert this code into a DIV. Subsequently, we scan this DIV for all script tags and execute the contents of these script tags using EVAL() (which add ...

Unlock the power of Shopify's API using node.js and React. Learn how to seamlessly read and write to the Shopify App API

I have recently delved into coding and Shopify, with some experience in fetching APIs using vanilla JavaScript. However, I find myself struggling to grasp the concept when it comes to node.js and React. Despite my best efforts to study the documentation, e ...

Duplicate multiple "li" elements using jQuery and place them in a designated position within the ul element, rather than at the end

I am currently working on developing a dynamic pagination bar. This pagination bar will dynamically clone the "li" elements based on a number received from an external webservice. Here is the structure of my pagination element: <ul class="pagination"& ...

Evolving fashion trends

I'm looking to dynamically change the style of my HTML element based on screen size, similar to this example: <p [ngStyle]="{'color': isMobile() ? 'red' : 'blue'}">Lorem Ipsum</p> The code above triggers a m ...

Utilizing dual functions within the onChange event handler in React

I have a situation where I need to pass a function from a parent component to a child component through the onChange event, as well as another function in the child component to update its own state. How can I achieve this? Parent export function Fruits() ...

When aligning to the right, input box does not wrap

Is there a way to prevent an input box from displaying on a lower line than the menu when using CSS to create a one line menu floated to the right? This is the CSS code being used: <style type="text/css"> #nav { margin:0; padding:0; lis ...

Enhanced JavaScript Autocompletion in Integrated Development Environments

What is the inner workings of Intellisense in IDEs like Webstorm and Eclipse for JavaScript? From which source do the suggestions originate? Is it possible to tweak the code to enhance the accuracy of the suggestions? https://i.sstatic.net/ZVBB3.png ...

The CSS menu dropdown fails to function properly on desktop view when there is longer content present

I attempted to merge two different navigation bars, one sticky and the other responsive. The goal was to combine https://www.w3schools.com/howto/howto_js_navbar_sticky.asp with https://www.w3schools.com/howto/howto_js_responsive_navbar_dropdown.asp Curr ...

concealing the port number in node.js and express using .htaccess

Currently, I am running two separate node servers on my website. One server is a socket.io server responsible for live streaming data on the main site, while the other server is streaming JSON data to a sub-domain called api.site.com. The main node server ...

What steps can be taken to update the cart if all products have been removed?

How can I implement a refresh for my cart page every time the product length is 0 without causing unreachable code? It seems like there must be a simple solution to this problem. switch (action.type){ case "REMOVE_PRODUCT": return ...

What causes the lack of impact on lambda rendering speed despite integrating webpack?

Hey there, I've been working on implementing webpack for a project that involves microservices, Node.js, TypeScript, AWS, and AWS SAM. My main objectives are: Reduce the cold start time of lambda functions. Minimize security vulnerabilities by e ...

When viewing my project on GitHub pages, the images within the card are not appearing

After running my project page link at https://nayaksofia.github.io/RestaurantReviewTest1/, I've noticed that the images inside the card are not displaying. However, when I run the same project on my local server localhost:8000, the images appear just ...

Text reveal animation in Framer Motion is not functioning as expected

I'm having trouble locating the issue in my code, and the text reveal feature isn't working. Interestingly, a simple animation works fine, indicating that there's no problem with the import or library: import { motion } from ...

The @input function in Vue.js is currently only triggered after the user has focused out, but I need it to be called while the user is

When working with @input on an input field in Vue.js, I am facing an issue where the assigned function is only called after the user has stopped typing and the focus is out of the input field. Essentially, it is triggered on onFocusout. What I actually wan ...

Update the table in GSP using the controller

Currently in my Grails project, I have a GSP page with a JQuery table that has buttons to add and remove rows. The code for this was taken from the tutorial on here. I successfully saved the table values into the database using the relevant controller&apo ...