0

这可能是一个愚蠢的问题,但我有一个问题不是代码无法运行 - 而是代码太长了......

一切正常,但例如有没有办法缩短if/else?或者是否可以使用更短的代码跟踪 4 个不同的var ?

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();
});
4

2 回答 2

2

三元运算符使其更具可读性:

transitionState_N  = (transitionState_N == '-') ? '+' : '-';

您的功能确实是重复的。寻找您经常重复的代码并编写一个函数来替换它。

于 2013-02-12T20:45:57.270 回答
0

这是我要做的:首先,您可以将 click() 函数分配给所有元素。然后,您可以获取每个元素的 id 并提取最后一个数字字符。

$('#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();
});

现在所有这些都将调用相同的函数:

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}
     );
};

您可能需要处理语法,但您会明白一般的想法

于 2013-02-12T21:30:42.587 回答