4

As the title states I want to add a fadeout animation once an image in my slideshow swaps only using html, css and javascript. I'm not quiet sure how to do this, but I have some idea. I was thinking I could add an id on the current image, like #fadeout so it gets specific characteristics when fadeing out.

var myIndex = 0;
window.onload = slidePictures();

function slidePictures() {
  var i;
  var slides = document.getElementsByClassName("mySlides");

  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
    slides[i].setAttribute("id", "fadeout");

  }
  myIndex++;
  if (myIndex > slides.length) {
    myIndex = 1
  }
  slides[myIndex - 1].style.display = "block";
  document.getElementById("indicator").innerHTML = myIndex + "/" + slides.length;

  setTimeout(slidePictures, 3000);

}
.slidesDiv>img {
  width: 80%;
  height: 80%;
  margin-left: 10%;
  opacity: 1;
  transition: opacity 1s;
}

#fadeOut {
  opacity: 0;
}
<div class="slidesDiv">
  <img class="mySlides" src="//placehold.it/200x80/0fb">
  <img class="mySlides" src="//placehold.it/200x80/0bf">
  <img class="mySlides" src="//placehold.it/200x80/fb0">
  <img class="mySlides" src="//placehold.it/200x80/0fb">
  <h1 id="indicator"> Indicator </h1>
</div>

2 Answers 2

3
  1. You shouldn't you display property with transition, because it isn't animatable.
  2. I recommend using position: absolute and hiding elements on init.
  3. I think it's better to combine fade in/fade out effects.

Try this example:

var indexes = {current: 0};
var slides = document.getElementsByClassName('mySlides');
window.onload = slidePictures();

function slidePictures() {
    if (indexes.last) {
    	slides[indexes.last].classList.remove('visible');
    }

    slides[indexes.current].classList.add('visible');

    document.getElementById('indicator').innerHTML = (indexes.current + 1) + '/' + slides.length;
    
    indexes.last = indexes.current;
    indexes.current++;
    if (indexes.current >= slides.length) {
        indexes.current = 0;
    }

    setTimeout(slidePictures, 3000);
}
html, body {
    width: 100%;
    height: 100%;
}

.slidesDiv {
    width: 80%;
    height: 80%;
    margin-left: 10%;
    position: relative;
}

.mySlides {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    transition: all 1s;
}

.visible {
    opacity: 1;
}
<div class="slidesDiv">
    <img class="mySlides" src="//placehold.it/200x80/0fb">
    <img class="mySlides" src="//placehold.it/200x80/0bf">
    <img class="mySlides" src="//placehold.it/200x80/fb0">
    <img class="mySlides" src="//placehold.it/200x80/bbb">
</div>
<h1 id="indicator">1/4</h1>

JSFiddle

Sign up to request clarification or add additional context in comments.

7 Comments

this worked. But it doesn't feel like the slideshow is inside the div? As in it can go outside of the div box? As in im trying to make a responsive website but the image just goes ontop of things because of the position: absolute
@Amar why? No, the slideshow is inside the div. What's your problem?
The positioning of the slideshow just feels off, when I minimize my website it just goes ontop of other things it shouldn't be able to
@Amar I don't see this problem in my snippet, it seems like the problem is in your site's code. Could you create the code snippet with the problem?
I mean as in this, ive got the borderbox around the div, But the width, height, margin etc of the slideshow doesn't adjust for the div instead it takes the whole screen if that makes sense. i.gyazo.com/7a6f08e9cd8982493978fceccca8a89c.png
|
1

I recommend using style attributes only. If you want to use CSS for applying a new style a new CSS class may be better. Then you could use slide.className += 'fadeOut'; .

Now the code for fading images:

function fadeIn(element) { element.style['display'] = '';
    element.style['opacity'] = 1; }

function fadeOut(element) {
    element.style['opacity'] = 0;
    setTimeout(function() { element.style['display'] = 'none'; }, YOUR_ANIMATION_DURATION_IN_MILLISECONDS);
}

function fadeBetween(from, to) {
    fadeIn(from);
    fadeOut(to);
}

That's the easiest I use to do it. Of course you could modify it to use CSS classes instead of style attributes. It should be easy to create a loop and cycle through all images which should be faded in our out.

8 Comments

Welcome to WebDevelopment. It depends on what you would like to do. Would you like to create an infinite slideshow starting on pageload?
In the for loop you should replace the current code with fadeOut(); and the ...style['display'] = 'block'; ... should be replaced with fadeIn()
That should be all.
What exatly should be in each of the parameters? ´fadeOut(slides[i]);´ ´fadeIn(slides[myIndex-1]);´ ?
Your code doesn't work for me, it doesn't give a fading effect.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.