Hello, I learning javascript and trying to reverse engineer something I found on a site to understand it.

Here is a link to the full script... http://jsfiddle.net/WHQKA/

I have gotten to understand most of it so I know what is going on and can learn from it.
However this line it confusing to me and I can not find any reference information. I am hoping that someone can simply explain it so I can grow.

$('.top:first').stop().animate({
    'left': (direction === 0) ? '-100%' : '100%' // I don't understand the infor after the colon :
},400);

So, I want to understand the direction and the negative and positve after that.

Many Thanks!
Tim

Recommended Answers

All 3 Replies

I assume you're asking about the ternary operator. This:

'left': (direction === 0) ? '-100%' : '100%'

Is roughly equivalent to this:

if (direction === 0) {
    'left': '-100%';
}
else {
    'left': '100%';
}

So it's just a expressionized version of an if..else statement.

Great! Thank you for the help. Still learning and understanding.
Appreciate your help!

In addition, that is a ternary operator in JavaScript, just want to let you know. It saves a lot of lines by the way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.