Change the body background image with fade effect in jquery

后端 未结 7 1669
说谎
说谎 2020-12-01 09:40

Hey i want to fade in a new background image let´s say every 60 seconds. I´ve set the background image like this:

body {background-image: url(background.jpg)         


        
7条回答
  •  不知归路
    2020-12-01 10:09

    Each 12 seconds an image is changed in presentacion container; it can be body tag

    HTML

    JS

    delay(11000) + setTimeout(1000) = 12 sec transition duration = 300 + 300 = 600 msec

    var imgArray = ['image-1', 'image-2', 'image-3'], index = 0;
    
    (function changeBG(){
      // mask element only for transition
      $('.mask').delay(11000).animate({opacity:1}, 300, function(){
        index = (index + 1) % img_array.length;
        // in presentacion element change bg images
        $('.presentacion').css("background-image", "url('images/bg-"+imgArray[index]+".jpg')");
      }).animate({opacity: 0}, 300);
      setTimeout(changeBG, 1000);
    })();
    

    CSS

    .presentacion {
      background-attachment: fixed;
      background-color: rgba(0, 0, 0, 0.5);
      background-image: url("images/image-1.jpg");
      background-position: center center;
      background-repeat: no-repeat;
      -webkit-background-size: cover;
      background-size: cover;
      position: relative;
      width: 100%;
      z-index: 0;
      opacity: 1;
    }
    .mask {
      background-color: rgba(0,0,0,0);
      bottom: 0;
      left: 0;
      opacity: 0;
      position: absolute;
      right: 0;
      top: 0;
      height: 100%;
      z-index: 10;
    }
    

提交回复
热议问题