日期:2014-05-16  浏览次数:20303 次

请帮忙精简一下下面的代码,菜鸟不太会for循环
Navigation.left = function()
{
Navigation.flag = false;
if(Nav_curItem ==9){
Nav_curItem = 8;
$("#box").animate({left:"222px"},"slow",function(){Navigation.flag = true;});
alert("8");
}
else if(Nav_curItem ==8){
Nav_curItem = 7;
$("#box").animate({left:"51px"},"slow",function(){Navigation.flag = true;});
alert("7");
}
else if(Nav_curItem ==7){
Nav_curItem = 6;
$("#bar").animate({left:"51px"},"slow");
$("#box").animate({left:"1077px"},"slow",function(){Navigation.flag = true;});
alert("6");
}
else if(Nav_curItem ==6){
Nav_curItem = 5;
$("#box").animate({left:"906px"},"slow",function(){Navigation.flag = true;});
alert("5");
}
else if(Nav_curItem ==5){
Nav_curItem = 4;
$("#box").animate({left:"735px"},"slow",function(){Navigation.flag = true;});
alert("4");
}
else if(Nav_curItem ==4){
Nav_curItem = 3;
$("#box").animate({left:"564px"},"slow",function(){Navigation.flag = true;});
alert("3");
}
else if(Nav_curItem ==3){
Nav_curItem = 2;
$("#box").animate({left:"393px"},"slow",function(){Navigation.flag = true;});
alert("2");
}
else if(Nav_curItem ==2){
Nav_curItem = 1;
$("#box").animate({left:"222px"},"slow",function(){Navigation.flag = true;});
alert("1");
}
else if(Nav_curItem ==1){
Nav_curItem = 0;
$("#box").animate({left:"51px"},"slow",function(){Navigation.flag = true;});
alert("0");
}
else if(Nav_curItem == 0)
{
Navigation.flag = true;
}
}

------解决方案--------------------
JScript code

Navigation.left=function(){
  Navigation.flag=false;
  var a=new Array(0,51,222,393,564,735,90,1077,51,222);
  if (Nav_curItem==0){Navigation.flag = true;  return;}
  var i = 0;
  for(i=9;i>0;i--){        
    if(Nav_curItem ==i){
      Nav_curItem=i-1;
      if(i==6){$("#bar").animate({left:"51px"},"slow");}
      $("#box").animate({left:ai+"px"},"slow",function(){Navigation.flag = true;});
      alert(Nav_curItem);
      return ;
    }
  }
}

------解决方案--------------------
JScript code
Navigation.left = function(){ 
    Navigation.flag = false;
    var left=51+171*(Nav_curItem>7?Nav_curItem-8:Nav_curItem-1);
    if(Nav_curItem==0){
        Navigation.flag = true;
    }else{
        Nav_curItem--;
        $("#box").animate({left:left+'px'},"slow",function(){Navigation.flag = true;});
    };
};