My Caption<\/script>')

Select the following image in jQuery

I have the following HTML markup.

<div class="caption">
    <img class='active' alt='My Caption' src='path/to/image' />
    <div>My Caption</div>
</div>
<div class="caption">
    <img alt='My Caption 2' src='path/to/image' />
    <div>My Caption 2</div>
</div>

Signatures are placed inside these DIVs with alt tags with some JS.

        $('#slider img').each(function() {
            $(this).wrap('<div class="caption"></div>').after('<div>' + $(this).attr('alt') + '</div>');
            $('.caption').width($(this).width()).children('div').each(function() {
                $(this).css({width: $(this).siblings('#slider img').width() -10, opacity: 0.5, display: 'block'});
            });
        });

The problem I am facing is trying to make a slide for my images, it worked fine, but now I have added captions in which I am having problems.

Here's what should happen:
It goes through each image, setting the class to an active and fading image, it worked fine without captions, but now that I have the captions, it puts the active class in a div with the caption.

Here is my js

function simpleSlider(){
    var $active = $('#slider img.active');

    if($active.length == 0) $active = $('#slideshow img:last');

    var $next = $active.next('img').length ? $active.next()
        : $('#slider img:first');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active');
            $active.css({opacity: 0.0});
        })
}

How can I do this, select the next image, not the div that says My Caption?

Sorry if that doesn't make much sense.

+3
source share
2

:

var $next = $active.next('img').length ? $active.next()
        : $('#slider img:first');

, next ( $active.next('img').length 0 , ). , .

:

function simpleSlider() {


    var $active = $('.caption.active');

    if ($active.length == 0) $active = $('.caption:last');

    var $next = $active.next('.caption').length ? $active.next() : $('.caption:first');

    $next.css({
        opacity: 0.0
    }).addClass('active').animate({
        opacity: 1.0
    }, 1000, function() {
        $active.removeClass('active');
        $active.css({
            opacity: 0.0
        });
    })
}

.

, , :

. $active.removeClass('active')

. CSS:

.caption{
z-index: 100;
}
.caption:not(.active){
z-index: 0;
}

, - :not() (.. IE), .

+3

.next() .caption, img. . , , :

function simpleSlider(){
    var $active = $('#slider img.active');
    if ($active.length == 0) $active = $('#slideshow img:last');
    var $parent = $active.closest(".caption");

    var $nextParent = $parent.next(".caption");
    if (!$nextParent.length) {
        $nextParent = $parent.siblings(".caption").eq(0);
    }
    var $next = $nextParent.find("img");

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active');
            $active.css({opacity: 0.0});
        })
}
+3

All Articles