jquery随机颜色

时间:2012-04-03 21:30:45

标签: jquery

嗨我正在尝试让这个脚本在li向上滚动时一个接一个地显示随机颜色但是到目前为止我已经做到了这一点但它不顺利它在重新启动时会改变所以你没有得到平滑的效果它改变颜色两次任何帮助将是非常感谢。

 $("#carousel ul").animate({marginTop:-100},2000,function(){

    function pad(s,i,c,r){
    i=i+1-s.length;
    if(i<1)return s;
    c=new Array(i).join(c||" ");
    return r?s+c:c+s;
    };

    hue  = "#"+pad((Math.random()*0x1000000<<0).toString(16),6,"0");
    hue2 = "#"+pad((Math.random()*0x1000000<<0).toString(16),6,"0");
    hue3 = "#"+pad((Math.random()*0x1000000<<0).toString(16),6,"0");

    $(this).find("li:last").after($(this).find("li:first"));
    $('#div1').css({backgroundColor: hue});
    $('#div2').css({backgroundColor: hue2}); 
    $('#div3').css({backgroundColor: hue3});

    $(this).css({marginTop:0});
    })  
    },1000);  
});

我的例子可以在http://swipedstudio.com/jtoy/找到 提前谢谢!

1 个答案:

答案 0 :(得分:0)

工作代码:

var t = setInterval(function(){ 

 $("#carousel ul").animate({marginTop:-100},2000,function(){

    function pad(s,i,c,r){
    i=i+1-s.length;
    if(i<1)return s;
    c=new Array(i).join(c||" ");
    return r?s+c:c+s;
    };

    hue  = "#"+pad((Math.random()*0x1000000<<0).toString(16),6,"0");

    $(this).find("li:last div").css({backgroundColor: hue});
    $(this).find("li:last").after($(this).find("li:first"));

    $(this).css({marginTop:0});
    })  
    },1000); 

问题:

  • 当你真的只想改变列表中的最后一个时,你正在改变每个区间的每个div的颜色
  • 当您更改最后一个div的颜色时,需要使用$(li:last div)选择它,因为三个div在列表中循环。特别选择div1,div2或div3时,它可能位于列表中的任何位置
  • 需要在$(li:last div)语句之前进行颜色更改(至少使用after()选择器)。您的原始代码会更改div的顺序,然后更改颜色
相关问题