当链接太多而且颜色数组太短时,如何管理数组从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') );
});
});
答案 0 :(得分:1)
这样做:
hover_color_array[i % hover_color_array.length]
这应该给你一个范围,它将始终落在数组中,因为它需要余数,其范围只能从0
到hover_color_array.length-1
答案 1 :(得分: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