dimanche 17 mai 2015

Change selection of current li element

I have a list:

<div id="slider">
<button id="prev" type="button">prev</button>
<button id="next" type="button">next</button>
<ul id="lol">
    <li id="a">1</li>
    <li id="b">2</li>
    <li id="c">3</li>
    <li id="d">4</li>
</ul>

and jquery code:

var $curr = $('#lol').children().first()
var $last = $('#lol').children().last()
$curr.css( "z-index", "1" );

$( "#next" ).click(function() {
    if ($curr == $last ) {
         $curr = $curr.first();
         $( "li" ).css( "z-index", "" );
         $curr.css( "z-index", "1" );
    } else {
         $curr = $curr.next();
         $( "li" ).css( "z-index", "" );
         $curr.css( "z-index", "1" );
    }
});

After clicking the next button i'd like to change style of next li item, but if it's a last li element of ul list it should change a first li element.

I dont't know how to chceck if $curr is the last one element of the list and go to first one. Please help :)

Aucun commentaire:

Enregistrer un commentaire