标签随机文字颜色和字体大小的实现方法

先上效果图:

实现代码:

<script type="text/javascript">
    $(document).ready(function(){
      var obj=$("#wrap a");//获取a标签中的数据
      function rand(num){
      //parseInt();将字符串转为整数
      //Math.random();生成随机数
      return parseInt(Math.random()*num+1);
      }
   
      function randomcolor(){
        var str=Math.ceil(Math.random()*16777215).toString(16);
        if(str.length<6){
        str="0"+str;
        }
        return str;
      }
   
      for(len=obj.length,i=len;i--;){
        obj[i].style.left=rand(600)+"px";//标签左右间距
        obj[i].style.top=rand(400)+"px";//标签上下间距
        obj[i].className="color"+rand(5);
        obj[i].style.zIndex=rand(5);//设置元素的堆叠顺序
        obj[i].style.fontSize=rand(5)+18+"px";//随机字体大小这里是18-23
        obj[i].style.color="#"+randomcolor();//字体颜色
        obj[i].style.padding=rand(15)+"px";
      }
    });
</script>

html中直接在div设置id=“wrap”,用<c:forEach>遍历标签。

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务