开发者

Fading between images

How can i fade rotate between two images using jQuery? Say for example.

<img src="image01.jpg" />
<img src开发者_StackOverflow="image02.jpg" />

I like the images to fade in/out between for say 2seconds each.

Thanks! Much appreciated. :)


A simple example i knocked up.

<div id="imageSwap">
</div>  


 <script language="javascript" type="text/javascript">

    $(document).ready(function () {
      setImageOne();
    });

    function setImageOne() {
      $('#imageSwap').fadeIn(500).html('<img src="image01.jpg" />').delay(2000).fadeOut(500, function () { setImageTwo(); });
    }

    function setImageTwo() {
      $('#imageSwap').fadeIn(500).html('<img src="image02.jpg" />').delay(2000).fadeOut(500, function () { setImageOne(); });
    }

  </script>


rewrite your html to

<div>
<img class="current slide" src="image01.jpg" style="position:absolute;"/>
<img class="slide" src="image02.jpg" style="position:absolute;display:none"/>
<img class="slide" src="image03.jpg" style="position:absolute;display:none"/>
<img class="dummy" src="image01.jpg" style="visibility:none;"/>
</div>

I added a 3rd image for clarity. Add a jquery function like

function nextSlide() {
    jQuery('.slide.current').each(function() {
        var next = jQuery(this).next('.slide');
        if (!next.size()) next = jQuery(this).siblings('.slide').eq(0);
        jQuery(this).fadeOut().removeClass('current');
        next.fadeIn().addClass('current');
    }); 
    slidetimer=setTimeout('nextSlide()',slidespeed);
}

And in your base javascript, add

var slidespeed = 2000;
var slidetimer = null;
jQuery(document).ready(function() {
    nextSlide()
});

I didnt test this exactly like this, so typoos may be there. The surrounding div ensures that all slide images are located on the spot where the div is located, because of their position:absolute. the dummy image is not visible, but does fill up the space needed to make the surrounding div actually wrap around all the images. You may not need this.

Your images should have similar sizes, or at least the dummy should be as big as the biggest image.

$2c, *-pike


You can do it without jquery. Only with css : And cool fade between image

.fade img {
  transition: all .8s;
  max-width: 200px;
}
.fade:hover img:nth-child(2) { 
	opacity: 0;
}
.fade img:first-child { 
  position: absolute;
	z-index: -1;
}
<div class='fade'>
  <img src="https://image.freepik.com/free-vector/software-logo_1103-316.jpg">
  <img src='https://image.freepik.com/free-vector/m-letter-logo-template_1061-150.jpg'>
</div>


I was looking for a way to fade between webcam images without having that moment between displayed images. What I'm doing is setting the new image as a background image, fading out the enclosed image, setting the background image as the enclosed image, then making the enclosed image visible again.

You could do something

I came up with this.

<style type="text/css">
    #webcamImageDiv {
        text-align: center;
        background-image: url('webcam.jpg');
        background-position: top center;
        background-repeat: no-repeat;
    }
</style>

<div id="webcamImageDiv">
    <img id="webcamImageImg" src="webcam.jpg" alt="Image Not Available" />
</div>

<script type="text/javascript">

    var refreshMillis = 10000;

    function refreshWebcam() {

      // set the div to the height of the image
      $("#webcamImageDiv").height( $("#webcamImageImg").height() );

      // get the path to the new image
      // (in your case, your other image)
      var newImg = "webcam.jpg?t=" + new Date().getTime();

      // set the background of the div
      $("#webcamImageDiv").css("background-image","url('" + newImg + "')");

      // fade out the image
      $("#webcamImageImg").fadeOut(500, function() {

        // update the img tag, show the image
        $("#webcamImageImg").removeAttr("src").attr("src", newImg).show();

        // do it again
        window.setTimeout(getWebcam, refreshMillis);
      });
    }

    $(document).ready( function() {
      window.setTimeout(refreshWebcam, refreshMillis);
    });
</script>


Better control of timing by using .animate and .css instead

This will create a reel an soft crossfade.

At each cycle, it moves the invisible image in front than start animation opacity to 100. Once done the previous visible image is then set to opacity 0 and move to front, etc.

<div>
        <div id="imageSwapA"  style="display:block; z-index:100; opacity:0;">
                <img src="imagea.png"/>
        </div>
        <div id="imageSwapB" style="display:block; z-index:101; opacity:0;">
                <img src="imagea.png"/>
        </div>
</div>

<script>
    $(document).ready(function () {

        setRollOne(); // <- animation can be triggered 

        function setRollOne() {
            $('#imageSwapA').css({'z-index':101}).animate({'opacity':1}, 3500, function(){
                    $('#imageSwapB').css({'opacity':0});
                    $('#imageSwapA').css({'z-index':100});
                    setTimeout(function(){ setRollTwo(); }, 1500);
            });
        }
        function setRollTwo() {
            $('#imageSwapB').css({'z-index':101}).animate({'opacity':1}, 3500, function(){
                    $('#imageSwapA').css({'opacity':0});
                    $('#imageSwapB').css({'z-index':100});
                    setTimeout(function(){ setRollOne(); }, 1500);
            });
        }
    });
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜