Reputation: 59
that's probably a silly question, but i have a problem not with the code not functioning - it's rather the code is too long...
everything works fine but for example is there a way to shorten the if/else ? or is it possible to track 4 different var but with a shorter code ?
var transitionState_N = '-';
var transitionState_X = '-';
var transitionState_Y = '-';
var transitionState_Z= '-';
// carousel 4way
function carousel_4way_N (n, el, m) {
var carousel = '#carousel-4way-nav-' + n
var carousel_content = '#carousel-4way-' + n
if (transitionState_N == '-')
{
transitionState_N = '+';
}
else
{
transitionState_N = '-';
}
$(carousel_reset).hide();
$(carousel).transition(
{y: m + '=10',delay:200}
);
};
function carousel_4way_X (n, el, m) {
var carousel = '#carousel-4way-nav-' + n
var carousel_content = '#carousel-4way-' + n
if (transitionState_X == '-')
{
transitionState_X = '+';
}
else
{
transitionState_X = '-';
}
$(carousel_reset).hide();
$(carousel).transition(
{y: m + '=10',delay:200}
);
};
function carousel_4way_Y (n, el, m) {
var carousel = '#carousel-4way-nav-' + n
var carousel_content = '#carousel-4way-' + n
if (transitionState_Y == '-')
{
transitionState_Y = '+';
}
else
{
transitionState_Y = '-';
}
$(carousel_reset).hide();
$(carousel).transition(
{y: m + '=10',delay:200}
);
};
function carousel_4way_Z (n, el, m) {
var carousel = '#carousel-4way-nav-' + n
var carousel_content = '#carousel-4way-' + n
if (transitionState_Z == '-')
{
transitionState_Z = '+';
}
else
{
transitionState_Z = '-';
}
$(carousel_reset).hide();
$(carousel).transition(
{y: m + '=10',delay:200}
);
};
$('#carousel-4way-nav-1').click(function(event){
$(carousel_reset_nav).not(this).transition(
{y:0});
return carousel_4way_N(1, this, (transitionState == '-') ? '+' : '-');
event.preventDefault();
});
$('#carousel-4way-nav-2').click(function(event){
$(carousel_reset_nav).not(this).transition(
{y:0});
return carousel_4way_X(2, this, (transitionState == '-') ? '+' : '-');
event.preventDefault();
});
$('#carousel-4way-nav-3').click(function(event){
$(carousel_reset_nav).not(this).transition(
{y:0});
return carousel_4way_Y(3, this, (transitionState == '-') ? '+' : '-');
event.preventDefault();
});
$('#carousel-4way-nav-4').click(function(event){
$(carousel_reset_nav).not(this).transition(
{y:0});
return carousel_4way_Z(4, this, (transitionState == '-') ? '+' : '-');
event.preventDefault();
});
Upvotes: 0
Views: 100
Reputation: 6933
Here is what I would do: for one, you can assign click() function to all elements. Then, you can get the id of each element and extract the last character that is a digit.
$('#carousel-4way-nav-1, #carousel-4way-nav-2,#carousel-4way-nav-3, #carousel-4way-nav-4').click(function(event){
$(carousel_reset_nav).not(this).transition(
{y:0});
return carousel_4way(($(this).attr('id')).substring(this.length -1), this, (transitionState == '-') ? '+' : '-');
event.preventDefault();
});
Now all of this will call the same function:
function carousel_4way(n, el, m) {
var carousel = '#carousel-4way-nav-' + n
var carousel_content = '#carousel-4way-' + n
switch(n){
case 1:
transitionState_N = (transitionState_N == '-') ? '+' : '-';
case 2:
transitionState_X = ...
...
}
$(carousel_reset).hide();
$(carousel).transition(
{y: m + '=10',delay:200}
);
};
you might need to work on a syntax, but you get the general idea
Upvotes: 0
Reputation: 298256
The ternary operator makes it somewhat more readable:
transitionState_N = (transitionState_N == '-') ? '+' : '-';
Your functions are really repetitive. Look for code you repeat often and write a function to replace it.
Upvotes: 2