Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

jquery - How to fade loop gallery background images

I wish to rotate a background image of a div which I believe precludes me from using the excellant jQuery Cycle Plugin.

The code below is what I have so far but it does not behave as I would like, which is:

  • Fade out first image
  • Swap image with second image while not showing an image - (fails this Fade in second image)
  • And repeat into infinity ...and beyond.

<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js"></script> 
<link rel="stylesheet" type="text/css" href="/css/normalize.css"> 
<link rel="stylesheet" type="text/css" href="/css/result-light.css"> 

<style type='text/css'> 
#mydiv {

        width: 100%;
        height: 365px;
        margin-left: auto;
        margin-right: auto;
        position: relative;
        display: block;
        background: url(HP_jQuery1.jpg) center top no-repeat;
}
</style>

<script type='text/javascript'> 
$(window).load(function(){
var firstTime = true;
var arr = ["url(HP_jQuery2.jpg)", "url(HP_jQuery3.jpg)"];
    (function recurse(counter) {
        var bgImage = arr[counter];
        if (firstTime == false) {
            $("#mydiv").fadeOut("slow");
            $('#mydiv').css('background-image', bgImage);
            $("#mydiv").fadeIn("slow");
        } else {
            firstTime = false;
        }               
        delete arr[counter];
        arr.push(bgImage);
        setTimeout(function() {
            recurse(counter + 1);
        }, 3600);
    })(0);      
});
</script> 

</head> 
<body> 
   <div id="mydiv">&nbsp;</div>
</body> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use-case jsBin Demo

In order to Cross-fade a background image, you'll need an extra layer to overlay your gallery.
Here's an example of the logic involved:

Fade loop background images

So create a DIV an place into it normal <img> tags for the browser to pre-load images, we'll use their src afterwards to use as covered background-image for the two elements:

jQuery(function($) {

  var $gallery = $("#gallery"); // Your HTML gallery
  var $overlay = $("<div/>");   // An overlay to X-fade
  var $images = $gallery.find("img");
  var n = $images.length; // How many images we have
  var c = 0; // Just a counter to loop using Reminder Operator %

  $gallery.css({backgroundImage: "url("+ $images[c].src +")"}).append( $overlay );

  (function loopBg(){
    $overlay.hide().css({backgroundImage: "url("+ $images[++c%n].src +")"}).delay(2000).fadeTo(1200, 1, function(){
      $gallery.css({backgroundImage: "url("+ $images[c%n].src +")"}); 
      loopBg();
    });
  }());

});
html, body{
  height:100%; margin:0; /*If needed*/ 
} 


#gallery,      /*Your gallery ID */
#gallery > div /*Immediate DIV child, created by jQuery*/
{
  position:absolute;
  width: 100%; height:100%;
  background: #000 none 50% / cover;
}
#gallery img{
  display:none;
  /*Exactly. We'll use them as BG images*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gallery">
  <!--
We'll not show images, those are just used for the browser to preload them
Let's just steal the images Sources and use them for the #gallery and #overlay backgrounds
-->
  <img src="http://dummyimage.com/600x365/fc5/fff&text=Image1" />
  <img src="http://dummyimage.com/600x365/cf5/fff&text=Image2" />
  <img src="http://dummyimage.com/600x365/f5c/fff&text=Image3" />
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...