$("#next").click(function() { button = current; current++ if (current == (images + 1)) { current = 1 } animateLeft(current, button) }); $("#previous").click(function() { button = current; current-- if (current == 0) { current = images } animateRight(current, button) }); function animateLeft(current, button) { $('#p' + current).css("left", width + "px"); $('#p' + current).animate({ "left": "0px" }, 400, "swing"); $('#p' + button).animate({ "left": -width + "px" }, 400, "swing"); setbutton() } function animateRight(current, button) { $('#p' + current).css("left", -width + "px"); $('#p' + current).animate({ "left": "0px" }, 400, "swing"); $('#p' + button).animate({ "left": width + "px" }, 400, "swing"); setbutton() } function setbutton() { $('#b' + button).css("backgroundPosition", "left top") $('#b' + button + ' b').css("color", "#16a"); $('#b' + current).css("backgroundPosition", "left bottom") $('#b' + current + ' b').css("color", "#fff"); }