views:

70

answers:

2

Hi,

Please tell me what variable is changing on the loop so I can maybe create a if else statement. I'm not a developer so, I really need help. Here's the code

$(document).ready(function(){

$("#health").show();
$("#health").hide();
$("#billing").hide();

var arr = [
    $("#pension"),
    $("#health"),
    $("#billing")
];
var cur = 0, nxt = 1;

function looptour(ncur){
if(ncur!=undefined) {
    arr[cur].hide();
    arr[ncur].show();
    cur = ncur;
    nxt = (cur + 1 < arr.length) ? cur + 1 : 0;
}
else {
setInterval(function() {
    arr[cur].fadeOut(2000);
    arr[nxt].fadeIn(2000);
    cur = (cur + 1 < arr.length) ? cur + 1 : 0;
    nxt = (nxt + 1 < arr.length) ? nxt + 1 : 0;
},6000);
}
}
looptour();

This is what I wanted to do... I just don't know the variable to use. Here's the idea, I have 3 buttons "1 2 3" I just want to add a class to those individual buttons 1 is for pension 2 is for health 3 is for billing

Thank you!

if() {
$("#tournums ul li:first a").addClass("num_active");
} else if() { $("#tournums ul li:eq(1) a").addClass("num_active");
} else if() { $("#tournums ul li:eq(2) a").addClass("num_active");
}
A: 

Hi, i don't know exactly what you want to do but I think this could help

if($("#health").attr("display") == "block")
{
    $("#tournums ul li a").addClass("num_active");
}
else
{
    $("#tournums ul li a").removeClass("num_active");
}

tell me what you want to do or give a link to the example at least.

geocine
I want to know what variable is changing from 0, 1, 2 ... so I can use that variable and maybe do the if-else statement
Eron
I tried your code, it is not working?
Eron
+2  A: 

EDIT: A little better than my original answer, since it caches the elements.

If I understand what you need correctly, I'd do this:

Try it out: http://jsfiddle.net/aTTrr/1

var arr = [
    $("#pension"),
    $("#health").hide(),
    $("#billing").hide()
];
var $aElements = $("#tournums ul li a");
var cur = 0, nxt = 1;

setInterval(function() {
    arr[cur].fadeOut(2000);
    arr[nxt].fadeIn(2000);
    $aElements.removeClass('num_active');
    $aElements.eq(nxt).addClass("num_active");
        // Modulus operator method courtesy of Nick Craver (see comment below)
    cur = (cur + 1)%arr.length;
    nxt = (nxt + 1)%arr.length;
},6000);

Original:

I assume the num_active is only on one element on the page at a time.

var arr = [
    $("#pension"),
    $("#health").hide(),
    $("#billing").hide()
];
var cur = 0, nxt = 1;

setInterval(function() {
    arr[cur].fadeOut(2000);
    arr[nxt].fadeIn(2000);
          // remove num_active class from the current one
    $('#tournums .num_active').removeClass('num_active');
          // add num_active class using nxt as the index
    $("#tournums ul li:eq(" + nxt + ") a").addClass("num_active");
    cur = (cur + 1 < arr.length) ? cur + 1 : 0;
    nxt = (nxt + 1 < arr.length) ? nxt + 1 : 0;
},6000);
patrick dw
+1 - Minor suggestion on the `cur`/`nxt` increment, use a modulus to do it a bit simpler: http://jsfiddle.net/nick_craver/aTTrr/2/
Nick Craver
You did it right! The problem is only on CSS. Thank you for this.
Eron
@Nick - Thank you for the tip! I was hoping for a shorter way of doing that. Very nice solution. :o)
patrick dw
I used the original code and btw, I just wonder. Why does my css is not working here? The background image is loading fine but the text color is not changing to white?Here's the sample:a.num_active { width: 16px; height: 19px; color: #FFF; background-image: url(../images/cdat_new/num-bg.jpg); }
Eron
@Eron - Your CSS looks fine to me. Does any of that CSS work? Or is it just the text color?
patrick dw
The text color only, anyway patrick. I figured it out. It is all fine now, thanks again for your help!
Eron