[jQuery] news rotator - looking for some help in cleaning up my code
I've got my code working (adapted of Karl Swedberg's Scroll Up News
Reader) http://docs.jquery.com/Tutorials:Scroll_Up_Headline_Reader,
but I'm sure there's a cleaner (more dynamic) way of checking where I
am in the loop so that I don't have to do all the individual click
checks
$("#topStoryButtons li a:eq(0)").click(function() {
$("#topStoryButtons li a:eq(1)").click(function() {
$("#topStoryButtons li a:eq(2)").click(function() {
$("#topStoryButtons li a:eq(3)").click(function() {
I think I'm just looking for a way to capture the instance of the link
I'm clicking and pass that variable on. Any help appreciated - Steve
/*====
* rotator
*================================================ */
var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;
$(document).ready(function(){
$("div#topStories #topStoryButtons").addClass("active");
headline_count = $("div.topStoryItem").size();
$("div.topStoryItem:eq("+current_headline+")").css('left','0');
$("#topStoryButtons li a:eq("+current_headline
+")").addClass('activeControls');
headline_interval = setInterval(headline_rotate,7000); //time in
milliseconds
$("#topStoryButtons li a:eq(0)").click(function() {
selected_headline = 0;
$("div.topStoryItem:eq(" + current_headline + ")").animate({left:
-622},"slow", function() {
$(this).css('left','622px');
});
$("#topStoryButtons li a:eq(" + current_headline +
")").removeClass('activeControls');
$("#topStoryButtons li a:eq(0)").addClass('activeControls');
$("div.topStoryItem:eq(0)").show().animate({left: 0},"slow");
current_headline = selected_headline;
clearInterval(headline_interval);
return false;
});
$("#topStoryButtons li a:eq(1)").click(function() {
selected_headline = 1;
$("div.topStoryItem:eq(" + current_headline + ")").animate({left:
-622},"slow", function() {
$(this).css('left','622px');
});
$("#topStoryButtons li a:eq(" + current_headline +
")").removeClass('activeControls');
$("#topStoryButtons li a:eq(1)").addClass('activeControls');
$("div.topStoryItem:eq(1)").show().animate({left: 0},"slow");
current_headline = selected_headline;
clearInterval(headline_interval);
return false;
});
$("#topStoryButtons li a:eq(2)").click(function() {
selected_headline = 2;
$("div.topStoryItem:eq(" + current_headline + ")").animate({left:
-622},"slow", function() {
$(this).css('left','622px');
});
$("#topStoryButtons li a:eq(" + current_headline +
")").removeClass('activeControls');
$("#topStoryButtons li a:eq(2)").addClass('activeControls');
$("div.topStoryItem:eq(2)").show().animate({left: 0},"slow");
current_headline = selected_headline;
clearInterval(headline_interval);
return false;
});
$("#topStoryButtons li a:eq(3)").click(function() {
selected_headline = 3;
$("div.topStoryItem:eq(" + current_headline + ")").animate({left:
-622},"slow", function() {
$(this).css('left','622px');
});
$("#topStoryButtons li a:eq(" + current_headline +
")").removeClass('activeControls');
$("#topStoryButtons li a:eq(3)").addClass('activeControls');
$("div.topStoryItem:eq(3)").show().animate({left: 0},"slow");
current_headline = selected_headline;
clearInterval(headline_interval);
return false;
});
});
function headline_rotate() {
current_headline = (old_headline + 1) % headline_count;
$("div.topStoryItem:eq(" + old_headline + ")").animate({left:
-622},"slow", function() {
$(this).css('left','622px');
});
$("#topStoryButtons li a:eq(" + old_headline +
")").removeClass('activeControls');
$("#topStoryButtons li a:eq(" + current_headline +
")").addClass('activeControls');
$("div.topStoryItem:eq(" + current_headline +
")").show().animate({left: 0},"slow");
old_headline = current_headline;
}