tags:

views:

63

answers:

1

Hello everyone. I'm developing a post slideshow for wordpress on jquery. I got to say I'm no expert and this is my first time working with jquery or javascript. Everything works perfect the code works just fine the post rotate each x seconds, there is two div's which move post left(previous) or right(next) on click, it works fine but on the first post (0) when you click left (previous) it shows nothing since 0 - 1 = -1 there is no -1 post and what is supossed to show is the last post (4). I tried if { } else if { } but as I said I'm no expert and I can't find a solution by myself. Any help is apreciated. The code...

$("#switch-izquierda").click(
 function() {
 $("#lista-contenedor-rotatorio li:eq("+img_actual+")").fadeOut(1600);
 img_actual = (img_actual -1) % cuenta;
 $("#lista-contenedor-rotatorio li:eq("+img_actual+")").fadeIn(2000);
 });

The problem is here...

 img_actual = (img_actual -1) % cuenta;

When in first post (0) result is -1 what I need is to show last post(4)

If you got a solution I really appreciate it.

+1  A: 

You don't want the remainder operator, but the modulo operator. I don't know if it exists in JS.

The easiest way may be to just to avoid the negative case by adding the length:

img_actual = (img_actual + cuenta - 1) % cuenta;
waxwing