0

This is what I've tried so far, but it just shows all the elements at once:

i1 = document.getElementById('img_1');
i2 = document.getElementById('img_2');
i3 = document.getElementById('img_3');
i4 = document.getElementById('img_4');
i5 = document.getElementById('img_5');

myarr = [i1,i2,i3,i4,i5];
    for (i=0; i<myarr.length;i++) {  
       $(myarr[i]).show().delay(5000).fadeOut();

    } 
Reez0
  • 2,512
  • 2
  • 17
  • 39

6 Answers6

3

I assume you are trying to achieve an endless loop.

I think you should use interval in that case, and do fadeOut/fadeIn of elements.

i1 = document.getElementById('img_1');
i2 = document.getElementById('img_2');
i3 = document.getElementById('img_3');
i4 = document.getElementById('img_4');
i5 = document.getElementById('img_5');

let myarr = [i1, i2, i3, i4, i5];
let active = 1;

setInterval(() => {
  $(myarr[active - 1]).fadeOut(500)
  if (active >= myarr.length) {
    active = 0
  }
  setTimeout(() => {
    $(myarr[active]).fadeIn(500);
    active = active + 1;
  }, 500)
}, 5000)

What this does, is updates elements every 5 sec to next element, if it reached the end, it resets it to zero.

Checkout this fiddle

noitse
  • 1,045
  • 9
  • 27
1

You can use async and await.

Another this you can improve is that. You can add same class to all images you want to show in series. If you want to select all by id you can use Attribute Selectors.

const myarr = document.querySelectorAll('img[id^=img]');

I have used same class rather than id

const arr = [...document.querySelectorAll('.test')];

(async function(){
    for (let i=0; i<arr.length;i++) {  
       await new Promise(res => {
          setTimeout(() => {
            $(arr[i]).show().fadeOut();
            res();
          },2000)
       }) 
    } 
})()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="test">Test 1</div>
<div class="test">Test 2</div>
<div class="test">Test 3</div>
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1

let count = 1;

setInterval(()=>{
document.querySelectorAll("*[id*='img_']").forEach((elem)=> elem.style.display="none");
document.getElementById(`img_${count}`).style.display="";
if(count<4) count++;
else count = 1;
},1000)
<div id="img_1">Image 1</div>
<div id="img_2" style="display:none">Image 2</div>
<div id="img_3" style="display:none">Image 3</div>
<div id="img_4" style="display:none">Image 4</div>
Vanilla Javascript solution!
Bilal Alam
  • 874
  • 10
  • 26
1

You forgot to show your element after fadeOut. Here you can achieve it:

// show first element
$('img').eq(0).show();
$('img').each(function () {
  // your delay
  $('img').delay(5000).fadeOut();
  // make sure next element is image
  if ($(this).next()[0].tagName === 'IMG') {
      // show next element
      $(this).next().fadeIn();
  }
});
img {
  display: none;
  position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://picsum.photos/id/5/50" />
<img src="https://picsum.photos/id/10/50" />
<img src="https://picsum.photos/id/30/50" />
<img src="https://picsum.photos/id/0/50" />
<img src="https://picsum.photos/id/150/50" />
<img src="https://picsum.photos/id/1000/50" />
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

var basicVal =0;
$(document).ready(function(){
   $('.wrapper img').eq( basicVal ).show();
   var setTime =setInterval(function(){
     if( basicVal < $('.wrapper img').length -  1){
       $('.wrapper img').eq(basicVal ).hide(); 
       basicVal++;
       $('.wrapper img').eq(basicVal).show();
     }else{
       clearTimeout(setTime);
     }    
     console.log();
     
   }, 5000);
});
.wrapper{
  width: 100%;
  float: left;
}
.wrapper img{
  width: 50%;
  height: 300px; 
  object-fit:  cover;
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper"> 
<img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="">
<img src="http://www.desktopwallpaperhd.net/wallpapers/0/4/landscapes-wallpaper-fengguangbizhi-fengjingbizhi-picture-image-1316.jpg" alt="">
<img src="http://trustbanksuriname.com/wp-content/uploads/2019/04/pony-picture-guide-to-native-pony-breeds-little-pony-cartoon-pictures.jpg" alt="">
<img src="https://www.bigfoto.com/stones-background.jpg" alt="">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQulscf1nNOpaI1tElZgKTTSAl_ZcL_i1VwLDojgKzqjSTMofsqPw" alt="">
  
  </div>

check this out I use some little bit of jquery and setInterval function to change in every 5000ms

Arindam Sarkar
  • 224
  • 1
  • 13
0

You may use setTimeout for achieving this effect.

<div id="container">
    <div class="block" id="img_1"></div>
    <div class="block" id="img_2"></div>
    <div class="block" id="img_3"></div>
    <div class="block" id="img_4"></div>
    <div class="block" id="img_5"></div>
</div>


.block{
    width:100px;
    height:100px;
    display: inline-block; 
    margin:10px;
    background: lightblue;  
    visibility: hidden;
}

And then,

$('.block').each(function(index, value) {  
    setTimeout(function() {
       $(value).css("visibility", "visible");
             $(value).show().delay(1000).fadeOut();
    }, 2000 * (index + 1));
});
Nuhman
  • 1,172
  • 15
  • 22