user20152015
user20152015

Reputation: 344

css animation not working with javascript code

I am trying to make a automatic Javascript Slideshow with fading animation based on w3 css that switches to manual when you hit the arrows. Besides this, in the beginning, if the previous or next arrows are not clicked, the slideshow will run for 2 loops. Clicking the previous or next arrows before 2 loops of slideshow finish also switches the slideshow to manual.

The issue is with the W3 Css fading animation. If I keep it, at the end of the automatic loops, the slides keep fading in and fading out while it should remain static. So, I have to remove the fading animation using the following code -

 Array.prototype.forEach.call(x, function(element) {
     element.classList.remove("w3-animate-fading"); //remove the fading
     });

Please point what am I doing wrong that the animation is not working properly with javascript (Please note - I am new to Javascript). Here is the full snippet including the remove animation code -

var index = 1;
var paused = false;
var slideShow = [];
var counter = 0;
var maxLength = 0;
var loops = 2;
var interval = 2000; //for testing purposes

 var x = document.getElementsByClassName("slideShow");
 maxLength = x.length * loops; //times 2 for two loops
 
for (i=0; i<x.length; i++) {
  slideShow[i] = document.getElementsByClassName("slideShow")[i];
  slideShow[i].style.display = "none";
}

slideShow[0].style.display = "block";

var slides = setInterval(function() {
counter++; //adding counter
   if (counter <= maxLength) //ie counter <= 10, execute 
  {
    if (index < slideShow.length) {
  
    index++;
		showDivs();
    
    }
  else {
		index = 1;
		showDivs();
	  }
  }
      Array.prototype.forEach.call(x, function(element) {
     element.classList.remove("w3-animate-fading"); //remove the fading
     });
 },interval);

function control(n) {
  clearInterval(slides);

  if (index+n > slideShow.length) {
    index = 1;
  }
  else if (index+n <= 0) {
    index = slideShow.length;
  }
  else {
		index += n;
  }
  showDivs();
}

function showDivs() {
  //Hide all slideShow elements, and then show only the targeted element
  for (i=1; i<=slideShow.length; i++) {
    slideShow[i-1].style.display = "none";
  }
  slideShow[index-1].style.display = "block";
}
.w3-content.w3-display-container {
  margin-top: 100px;
}
 
 button.w3-button.w3-display-left.w3-black {
  position: relative;
  left: -50px;
}
 
 button.w3-button.w3-display-right.w3-black {
  position: relative;
  right: -118px;
}
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"/>
<div class="w3-content w3-display-container " style="max-width:150px">

  <div class="w3-display-container w3-animate-fading slideShow">
      <a href="" 	target="_blank" title="slide1">
      <img class="" src="img01.jpg"	style="width:100%">
      </a>
      <div class="w3-display-bottomleft w3-large 	w3-container w3- padding-16 w3-black">
        <a href="" target="_blank" title="slide1">Slide-1 (read more)</a>
    </div>
  </div>

  <div class="w3-display-container w3-animate-fading slideShow">
        <a href="" target="_blank" title="slide2">
    <img class="" src="img02.jpg" style="width:100%">
    </a>	
    <div class="w3-display-bottomright w3-large w3-container w3-padding-16     w3-black">
        <a href="" target="_blank" title="slide2">Slide-2 (more)</a>
    </div>
  </div>

  <div class="w3-display-container w3-animate-fading slideShow">
        <a href="" target="_blank" title="slide3">
    <img class="" src="img03.jpg" style="width:100%">
    </a>
        <div class="w3-display-topleft w3-large w3-container 
           w3-padding-16 w3-black">
        <a href="" target="_blank" title="slide3">Slide-3 (read more)</a>
        </div>
  </div>  

  <div class="w3-display-container w3-animate-fading slideShow">
        <a href="" target="_blank" title="slide4">
    <img class="" src="img04.jpg" style="width:100%">
    </a>
        <div class="w3-display-topright w3-large w3-container 
           w3-padding-16 w3-black">
        <a href="" target="_blank" title="slide4">Slide-4 (read more)</a>
        </div>
  </div>

  <div class="w3-display-container w3-animate-fading slideShow">
      <a href="" target="_blank" title="slide5">
      <img class="" src="img05.jpg" style="width:100%">
      </a>
        <div class="w3-display-middle w3-large w3-container  	
           w3-padding-16 w3-black">
        <a href="" target="_blank" title="slide5">Slide-5 (read	more)</a>
        </div>
  </div>

  <button class="w3-button w3-display-left w3-black" onclick="control(-1)"><</button>
      <button class="w3-button w3-display-right w3-black" onclick="control(1)">></button>

</div>

Upvotes: 0

Views: 302

Answers (1)

Suriya Subramaniyan
Suriya Subramaniyan

Reputation: 137

Remove this,

 Array.prototype.forEach.call(x, function(element) {
 element.classList.remove("w3-animate-fading"); //remove the fading
 });

and add and remove fade class for active slide conditionally,

slideShow[0].className = "w3-animate-fading slideShow";
slideShow[index-1].className = "w3-animate-fading slideShow";

See snippet

var index = 1;
var paused = false;
var slideShow = [];
var counter = 0;
var maxLength = 0;
var loops = 2;
var interval = 2000; //for testing purposes

 var x = document.getElementsByClassName("slideShow");
 maxLength = x.length * loops; //times 2 for two loops
 
for (i=0; i<x.length; i++) {
  slideShow[i] = document.getElementsByClassName("slideShow")[i];
  slideShow[i].style.display = "none";
}

slideShow[0].style.display = "block";
slideShow[0].className = "w3-animate-fading slideShow";

var slides = setInterval(function() {
counter++; //adding counter
   if (counter <= maxLength) //ie counter <= 10, execute 
  {
    if (index < slideShow.length) {
  
    index++;
		showDivs();
    
    }
  else {
		index = 1;
    // Here add the parameter for identify loop ends
		showDivs(true);
	  }
  }
     
 },interval);

function control(n) {
  clearInterval(slides);

  if (index+n > slideShow.length) {
    index = 1;
  }
  else if (index+n <= 0) {
    index = slideShow.length;
  }
  else {
		index += n;
  }
  showDivs();
}

function showDivs(value) {
  //Hide all slideShow elements, and then show only the targeted element
  for (i=1; i<=slideShow.length; i++) {
    slideShow[i-1].style.display = "none";
  }
  slideShow[index-1].style.display = "block";
  
  if(value) { // Here check the condition if loop ends remove fade class from slide
  
      slideShow[index-1].classList.remove("w3-animate-fading");
      
  } else {  // otherwise add fade class to slide
         
      slideShow[index-1].className = "w3-animate-fading";
      
       // after that remove class after some time for remove fade
      setTimeout(function(){
          slideShow[index-1].classList.remove("w3-animate-fading");
      },1000);
  }
}
.w3-content.w3-display-container {
  margin-top: 100px;
}
 
 button.w3-button.w3-display-left.w3-black {
  position: relative;
  left: -50px;
}
 
 button.w3-button.w3-display-right.w3-black {
  position: relative;
  right: -118px;
}
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"/>
<div class="w3-content w3-display-container " style="max-width:150px">

  <div class="w3-display-container w3-animate-fading slideShow">
      <a href="" 	target="_blank" title="slide1">
      <img class="" src="img01.jpg"	style="width:100%">
      </a>
      <div class="w3-display-bottomleft w3-large 	w3-container w3- padding-16 w3-black">
        <a href="" target="_blank" title="slide1">Slide-1 (read more)</a>
    </div>
  </div>

  <div class="w3-display-container w3-animate-fading slideShow">
        <a href="" target="_blank" title="slide2">
    <img class="" src="img02.jpg" style="width:100%">
    </a>	
    <div class="w3-display-bottomright w3-large w3-container w3-padding-16     w3-black">
        <a href="" target="_blank" title="slide2">Slide-2 (more)</a>
    </div>
  </div>

  <div class="w3-display-container w3-animate-fading slideShow">
        <a href="" target="_blank" title="slide3">
    <img class="" src="img03.jpg" style="width:100%">
    </a>
        <div class="w3-display-topleft w3-large w3-container 
           w3-padding-16 w3-black">
        <a href="" target="_blank" title="slide3">Slide-3 (read more)</a>
        </div>
  </div>  

  <div class="w3-display-container w3-animate-fading slideShow">
        <a href="" target="_blank" title="slide4">
    <img class="" src="img04.jpg" style="width:100%">
    </a>
        <div class="w3-display-topright w3-large w3-container 
           w3-padding-16 w3-black">
        <a href="" target="_blank" title="slide4">Slide-4 (read more)</a>
        </div>
  </div>

  <div class="w3-display-container w3-animate-fading slideShow">
      <a href="" target="_blank" title="slide5">
      <img class="" src="img05.jpg" style="width:100%">
      </a>
        <div class="w3-display-middle w3-large w3-container  	
           w3-padding-16 w3-black">
        <a href="" target="_blank" title="slide5">Slide-5 (read	more)</a>
        </div>
  </div>

  <button class="w3-button w3-display-left w3-black" onclick="control(-1)"><</button>
      <button class="w3-button w3-display-right w3-black" onclick="control(1)">></button>

</div>

Upvotes: 1

Related Questions