开发者

jquery inline images fade-in fade-out along with caption in <span>

I'm using this code for Fade-in Fade-out of images http://snook.ca/archives/javascript/simplest-jquery-slideshow

jQuery

$(function(){
    $('.fadein img:gt(0)').hide();
    setInterval(function(){
      $('.fadein :first-child').fadeOut()
         .next('img').fadeIn()
         .end().appendTo('.fadein');}, 
开发者_如何转开发      3000);
});

CSS

.fadein { position:relative; width:500px; height:332px; }
.fadein img { position:absolute; left:0; top:0; }

HTML

<div class="fadein">
  <img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg">
  <img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg">
  <img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg">
</div>

This code works well with images. But I want to show different caption with each image.

My code will be this

<div class="fadein">
<p><span>Caption 1</span><img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg"></p>
<p><span>Caption 2</span><img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg"></p>
<p><span>Caption 3</span><img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg"></p>
</div>

How to modify the above JQuery code to achieve the fade-in fade-out images along with captions?

I want to show the caption over the image.

JsFiddle of current example

http://jsfiddle.net/7VkrG/1/

JsFiddle link with my required html code

http://jsfiddle.net/twx9T/1/


This seems to do what you want (you may have to edit the CSS to get the caption to display to your taste).

HTML:

<div class="fadein">
<p><img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg"><span>Caption 1</span> </p>
<p><img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg"><span>Caption 2</span> </p>
<p><img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg"><span>Caption 3</span> </p>
</div>

CSS:

body {font-family:Arial, Helvetica, sans-serif; font-size:12px;}

.fadein { position:relative; height:332px; width:500px; }
.fadein p { position:absolute; left:0; top:0; background-color:#fff; border:1px solid black; }

jQuery:

$(function() {
    $('.fadein p').hide();
    $('.fadein p:first-child').show();
    setInterval(function() {
        $('.fadein p:first-child').fadeOut(function(){
            $(this).appendTo('.fadein');
        }).next().fadeIn();
    }, 3000);
});

jsFiddle:

http://jsfiddle.net/sYhQC/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜