美文网首页
随机变颜色案例

随机变颜色案例

作者: LzW伟 | 来源:发表于2018-07-25 19:41 被阅读0次

div

<div id='div'></div>

css

<style>    
#div{
    width:100px;
    height:100px;
}
</style>

js

  <script>
  ####定义一个随机数的函数
    
    function rn(min,max){
      return  Math.floor(Math.random()*(max-min+1)+min);
    }
    
    ####定义随机颜色
    function rc(){
        var r=rn(0,255);
        var g=rn(0,255);
        var b=rn(0,255);
       return `rgb(${r},${g},${b})`
    }
    
  document.getElementById('div').style.background=rc();
</script>

相关文章

网友评论

      本文标题:随机变颜色案例

      本文链接:https://www.haomeiwen.com/subject/bremmftx.html