1

I have a block on a website I am working on which consists of some buttons and a background image.

I need to change the background image every 3 seconds so I have the following javascript :

jQuery('.myBlock').css({"background-image" : "url(img1)"});

var counter = 0;
function setBckImage(){
    if(counter<3){
        counter++;
    } else {
        counter=1;
    }

    switch (counter){
        case 1:
            jQuery('.myBlock').css({"background-image" : "url(img1)"});
            break;
        case 2:
            jQuery('.myBlock').css({"background-image" : "url(img2)"});
            break;
        case 3:
            jQuery('.myBlock').css({"background-image" : "url(img3)"});
            break;
    }
}

setInterval(setBckImage, 3000);

Which works, but the change is "sudden" and I would like it to be more smooth, adding a fadein/fadeout effect. How can I achieve this ? I saw this answer that lead to this link but in this link, the images are inside div, they are not backgroud-image, so I don't understand how to adapt this answer to my code.

Is there a way to achieve this ?

Community
  • 1
  • 1
DevBob
  • 835
  • 2
  • 9
  • 29

3 Answers3

2

Probably you want that effect?

$('.myBlock').css({"background-image" : "url(http://nakolenke.club/uploads/posts/2016-09/1473248821_kotiki04.jpg)"});

var counter = 0;
function setBckImage(){
    if(counter<3){
        counter++;
    } else {
        counter=1;
    }

    switch (counter){
        case 1:
            jQuery('.myBlock').css({"background-image" : "url(http://nakolenke.club/uploads/posts/2016-09/1473248821_kotiki04.jpg)"});
            break;
        case 2:
            jQuery('.myBlock').css({"background-image" : "url(http://nakolenke.club/uploads/posts/2016-09/1473248821_kotiki04.jpg)"});
            break;
        case 3:
            jQuery('.myBlock').css({"background-image" : "url(http://i.bigmir.net/img/dnevnik/uploads/cmu_1153/29306/1.jpg)"});
            break;
    }
}

setInterval(setBckImage, 3000);
.myBlock {
  width: 100px;
  height: 100px;
  background-size: cover;
   -webkit-transition: background 700ms ease-in 700ms;
    -moz-transition: background 700ms ease-in 700ms;
    -o-transition: background 700ms ease-in 700ms;
    transition: background 700ms ease-in 700ms;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myBlock"></div>
Boris P.
  • 356
  • 3
  • 13
1

You just try this

Use this code :)

<div id="dvImage" style="height: 308px; width: 410px">
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
  var images = ["Chrysanthemum.jpg", "Desert.jpg", "Hydrangeas.jpg", "Jellyfish.jpg", "Koala.jpg", "Lighthouse.jpg", "Penguins.jpg", "Tulips.jpg"];
  $(function () {
      var i = 0;
      $("#dvImage").css("background-image", "url(images/" + images[i] + ")");
      setInterval(function () {
          i++;
          if (i == images.length) {
              i = 0;
          }
          $("#dvImage").fadeOut("slow", function () {
              $(this).css("background-image", "url(images/" + images[i] + ")");
              $(this).fadeIn("slow");
          });
      }, 1000);
  });
</script>
Karthick Nagarajan
  • 1,327
  • 2
  • 15
  • 27
1

One way to do this is with css keyframes, you can add this to .myBlock

.myBlock{
  animation: fadeInOut 3s infinite;
}

@keyframes fadeInOut{
  0%{
    opacity: 0;
  }
  20%{
    opacity: 1;
  }
  80%{
    opacity: 1;
  }
  100%{
    opacity: 0;
  }
}

But keep in mind, if you change the 3 seconds interval, you have to change it in the css as well.