views:

1713

answers:

2

Hello,

I have a jQuery problem, and I've really tried everything I know (i am so newbie at this) so ..

The problem in short is I am doing a simple carousel-like effect. I am using this code.

(The div .showarea is the DIV which needs to be rotated (next/prev) but I want to keep only one div shown at a time.)

This is the html markup.

    <div class="maincarousel">
       <div class="showarea"><a href="#"><img src="img/sampleshow.png" alt="" title="" /></a></div>
       <div class="showarea"><a href="#"><img src="img/sampleshow2.png" alt="" title="" /></a></div>
       <div class="showarea"><a href="#"><img src="img/sampleshow3.png" alt="" title="" /></a></div>
       <a href="#carouselPrev" class="leftarrow" title="Previous"></a>
       <a href="#carouselNext" class="rightarrow" title="Next"></a>
    </div>

This is my jquery attempt

$('.showarea').hide();
$('.showarea:first').fadeIn(500);

$('.maincarousel a').click(function () {

    if ($(this).attr('href') == '#carouselNext') {
        $('.showarea').hide();
        $('.showarea').next('.showarea').fadeIn(500);
    }

    if ($(this).attr('href') == '#carouselPrev') {
        $('.showarea').hide();
        $('.showarea').prev('.showarea').fadeIn(500);
    }

});

Unfortunately, next() and prev() won't display the next element only, but all next elements and same thing for prev(). Any quick workaround..

Can someone help me on this,

Thanks

+8  A: 

You could try using .eq(0) to select the first element in the collection given to you by .prev() and .next().

Note that .next() and .prev(), like most jQuery methods, are operating on a collection. So if your selector '.showarea' is selecting multiple elements, then .next() will select the next sibling element for each element selected by '.showarea', and similarly for .prev().


if ($(this).attr('href') == '#carouselNext') {
    $('.showarea').hide();
    var el = $('.showarea').next('.showarea').eq(0);
    if (el.length) {
        el.fadeIn(500);
    }

}

if ($(this).attr('href') == '#carouselPrev') {
    $('.showarea').hide();
    var el = $('.showarea').prev('.showarea').eq(0);
    if (el.length) {
        el.fadeIn(500);
    }
}
womp
Can I see a quick example of using get(0) please?
Ahmad Fouad
Note that get(0) returns the dom object. He will need .eq(0) to continue the chain.
redsquare
redsquare because I applied get(0) and it didn't work, so I thought i am missing something indeed :)
Ahmad Fouad
@womp, modified your post to include a code example
Andreas Grech
Gah, sorry I meant .eq(0). Been a couple weeks since I touched my client side stuff :/
womp
I do not know what i am missing. It stops after the first next element i suppose because we're using eq(0) so it displays one next element and one prev element, perhaps i didn't explain my question well enough :)
Ahmad Fouad
your probably after looking for the sibling elements within the context of the anchor that has been clicked on
redsquare
I guess not. You explicitly asked to display the next element only?
womp
@womp yes my mistake. :) I meant, displaying the next element only from a collection of elements. But when the next button is clicked again, it continues the chain.. like a series of images in carousel (say 10..or 20 images..) can be rotated by next/prev buttons. I've used next() / prev() and they display all images in the series. so what I wanted to achieve is displaying the first element from this series only. :) Sorry :)
Ahmad Fouad
+2  A: 

The below will rotate so if you are at the first item pressing back will then show the last item...

Demo here

$('div.showarea').fadeOut(0);
$('div.showarea:first').fadeIn(500);

$('a.leftarrow, a.rightarrow').click( function (ev) {
    //prevent browser jumping to top
    ev.preventDefault();

    //get current visible item
    var $visibleItem = $('div.showarea:visible');

    //get total item count
    var total =  $('div.showarea').length;

    //get index of current visible item
    var index = $visibleItem.prevAll().length;

    //if we click next increment current index, else decrease index
    $(this).attr('href') === '#carouselNext' ? index++ : index--;

    //if we are now past the beginning or end show the last or first item
    if (index === -1){
       index = total-1;
    }
    if (index === total){
       index = 0
    }

    //hide current show item
    $visibleItem.hide();

    //fade in the relevant item
    $('div.showarea:eq(' + index + ')').fadeIn(500);

});
redsquare
I am sorry. I included my markup now.
Ahmad Fouad
Wow you're awesome! :) :) It's perfect and really straightforward!
Ahmad Fouad