1

当链接太多且颜色数组太短时,如何管理数组从 0 重新启动(在函数 get_hover_color 中)?感谢您的帮助!

直到。

<div class="line">
<div><h2 class="color_link">DRUCKVORSTUFE</h2></div>
<div><h2 class="color_link">SCAN</h2></div>
<div><h2 class="color_link">RETUSCHEN</h2></div>
<div><h2 class="color_link">ANDRUCK</h2></div>
<div><h2 class="color_link">PDF</h2></div>
<div><h2 class="color_link">CTP</h2></div>
<div><h2 class="color_link">STACCATO</h2></div>
.............
.............
.............
........and so on........

</div>

function get_hover_color(i){

        hover_color_array = ['54cacd','e84d3a','ff3384','a0388d','231599','0d958a','629B39','33811E','F04E00','F00200','D9008F','58dbb4','093C8B','AECB27','E5B208','74204f','C26A21','DB2C17','cccf78','166C64','9A1E6E','7EADC2','00A1CE'];

return hover_color_array[i];

     }



$('.color_link').each(function(i){
            $(this).hover(
             function(){
               var $this = $( this );
               $this.data( 'prehovercolor', $this.css( 'color' ) ).css( {'color' : '#' + get_hover_color(i), 'cursor' : 'pointer' });
             },function(){
               var $this = $( this );
               $this.css( 'color', $this.data( 'prehovercolor') );
             });
        });
4

2 回答 2

1

做这个:

hover_color_array[i % hover_color_array.length]

这应该为您提供一个始终落在数组内的范围,因为它需要余数,其范围只能从0hover_color_array.length-1

于 2013-01-28T18:07:26.503 回答
0

考虑使用长度的模数:

function get_hover_color(i){

    hover_color_array = ['54cacd','e84d3a','ff3384','a0388d','231599','0d958a','629B39','33811E','F04E00','F00200','D9008F','58dbb4','093C8B','AECB27','E5B208','74204f','C26A21','DB2C17','cccf78','166C64','9A1E6E','7EADC2','00A1CE'];

k = i % hover_color_array.length;

return hover_color_array[k];

 }

http://msdn.microsoft.com/en-us/library/ie/9f59bza0(v=vs.94).aspx

于 2013-01-28T18:08:51.940 回答