html5随机圆不重叠,Js生成随机圆形,随机颜色,随机位置,不重叠

这篇文章主要是用来总结之前的那两篇文章的知识点,分别是《处理数组循环中删除元素导致索引错位情况》和《JavaScript规则图形碰撞原理》,还是用demo来总结来得实在,做了个生成随机圆形,随机颜色,随机位置,不重叠的小demo。demo的js代码如下:

function creatDiv(r_w){

var randomWidth=parseInt(Math.random()*r_w+50);

var allEle=document.getElementById("page467").getElementsByTagName("*");

var div=document.createElement("div");

var bottom=parseInt(Math.random()*(document.body.clientHeight-randomWidth));

var left=parseInt(Math.random()*(document.body.clientWidth-randomWidth));

div.setAttribute("class","lucky-circle");

div.setAttribute("data-left",left);

div.setAttribute("data-bottom",bottom);

div.style.bottom=bottom+"px";

div.style.left=left+"px";

div.style.backgroundColor="rgb("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+")";

div.style.width=randomWidth+"px";

div.style.height=randomWidth+"px";

for(let i=allEle.length-1;i>0;i--){

if(allEle[i].getAttribute("data-left")){

let tLeft=parseInt(allEle[i].getAttribute("data-left"));

let tBottom=parseInt(allEle[i].getAttribute("data-bottom"));

let width=allEle[i].offsetWidth;

let height=allEle[i].offsetHeight;

let point_x_1=tLeft+width/2;

let point_y_1=tBottom+height/2;

let point_x_2=left+randomWidth/2;

let point_y_2=bottom+randomWidth/2;

let distant= Math.sqrt(Math.pow(Math.abs(point_x_1-point_x_2),2)+Math.pow(Math.abs(point_y_1-point_y_2),2));

if(distant

allEle[i].parentNode.removeChild(allEle[i]);

}

}

}

document.getElementById("page467").appendChild(div);

}

document.οnkeydοwn=function(){

creatDiv(150);

};

点击查看demo

按Esc键退出,按其它任意键生成圆。

或者到我的GitHub里面获取源码。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue3中生成随机盒子,你可以使用grid布局和随机颜色来实现。首先,在你的template中,你可以使用ul和li标签来创建一个列表,将每个li标签作为一个盒子,然后使用v-for指令遍历一个包含随机颜色的数组。代码如下所示: ```html <ul class="label_list"> <li class="label_item" v-for="(item, index) in skillTagArr" :style="{ backgroundColor: item.color }"> <i class="el-icon-share"></i>{{ item.tag }} </li> </ul> ``` 接下来,在script标签中,你可以添加一个data属性来存储生成的随机颜色数组skillTagArr,并在mounted钩子函数中调用一个方法initTarget来初始化这个数组。在initTarget方法中,你可以将vuex仓库中的数据赋值给skillTagArr,并为每个对象添加一个color属性来存储随机生成的颜色。代码如下所示: ```javascript import { mapState } from 'vuex' export default { data() { return { skillTagArr: [], } }, mounted() { this.initTarget() }, methods: { initTarget() { this.skillTagArr = this.personalInfo.tags this.skillTagArr.forEach((item) => { item.color = this.RandomColor() }) }, RandomColor() { let r, g, b r = Math.floor(Math.random() * 256) g = Math.floor(Math.random() * 256) b = Math.floor(Math.random() * 256) return 'rgb(' + r + ',' + g + ',' + b + ')' }, }, computed: { ...mapState(['personalInfo']), }, } ``` 最后,你可以在style标签中定义grid-box容器以及盒子的样式。代码如下所示: ```css <style scoped> .grid-box { display: grid; grid-template-columns: auto auto auto auto auto auto; grid-gap: 20px; background-color: #2196F3; padding: 10px; box-sizing: border-box; width: 700px; height: 340px; } .grid-item { background: #ccc; } .def-box { background: #FFFFFF; } </style> ``` 通过以上的代码,你可以在Vue3中生成一个带有随机颜色的盒子网格。每个盒子的颜色都是通过RandomColor方法随机生成的,并且通过v-for指令循环生成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值