loops - jQuery looping prev next -
i found this bit of code whist searching basic slider , near enough want loop once got last li, i.e if last li , next button clicked loop 1st , continue,
all appreciated , in advance.
$('.udtalelse').addclass("passiv"); ctrlkunder(0); $('#prev').click(function() { var index = getactive(); if (index == 0) return; index--; ctrlkunder(index); }) $('#next').click(function() { var index = getactive(); if (index == $('.udtalelse').length - 1) return; index++; ctrlkunder(index); }) function ctrlkunder(ele) { $('.udtalelse').removeclass("active").addclass("passiv"); $('.udtalelse').eq(ele).removeclass("passiv").addclass("active"); } function getactive() { var index; $('.udtalelse').each(function(i) { if ($(this).hasclass("active")) index = i; }) return index; } <ul class="kunder"> <li><span class="udtalelse">indhold 1</span></li> <li><span class="udtalelse">indhold 2</span></li> <li><span class="udtalelse">indhold 3</span></li> </ul> <span class="kunderpagination"> <a href="#" id="prev">« previous</a> | <a href="#" id="next">next »</a> </span>
your code can simplified this:
var = $('.udtalelse').addclass("passiv"); var = -1; $('#prev').click(function() { ctrlkunder( = !i ? all.length - 1 : --i ); }); $('#next').click(function() { ctrlkunder( = ++i % all.length ); }).click(); function ctrlkunder(ele) { all.removeclass("active").addclass("passiv"); all.eq(ele).removeclass("passiv").addclass("active"); } also more efficient because .udtalelse elements cached.
example: http://jsfiddle.net/3z9uc/
edit: forgot update answer revision that's in fiddle. fixed.
Comments
Post a Comment