【发布时间】:2022-02-08 00:08:33
【问题描述】:
点击按钮点击我!如何避免随机数重叠? 它会生成随机数,但是有一些数字重叠如何避免这种情况......,这里我不是在谈论随机数的重复,我想避免随机数的重叠......
const circle = document.querySelector(".circle");
const addNumBtn = document.querySelector('#addNumBtn')
function randomValue() {
let random = Math.floor(Math.random() * 50) + 1;
return random;
}
function randomColor(){
let r = Math.floor(Math.random() * 255) + 1;
let g = Math.floor(Math.random() * 255) + 1;
let b = Math.floor(Math.random() * 255) + 1;
return `rgba(${r},${g},${b})`
}
function randomSize(){
let size = Math.floor(Math.random() * 30) + 8;
return `${size}px`
}
function randNum() {
let randnum = document.createElement("p");
randnum.classList.add('numStyle')
randnum.style.color=randomColor();
randnum.style.fontSize=randomSize();
randnum.textContent = randomValue();
randnum.style.position = 'absolute'
randnum.style.top = `${randomValue()}px`
randnum.style.left = `${randomValue()}px`
randnum.style.bottom = `${randomValue()}px`
randnum.style.right = `${randomValue()}px`
circle.append(randnum);
}
addNumBtn.addEventListener('click',randNum)
.circle {
aspect-ratio: 1;
width: 300px;
background-color: black;
border-radius: 50%;
position: relative;
overflow:hidden;
}
#addNumBtn{
position:absolute;
top:0;
left:0;
}
.numStyle{
font-family:"roboto",sans-serif;
}
//On Clicking button Click Me! I want to place all the random numbers inside the circle
//together not one by one, at a time all numbers should be placed inside the circle by
//clicking
the button Click Me!
<div id="container"></div>
<div class="circle"></div>
</div>
<button id="addNumBtn">Click Me!</button>
【问题讨论】:
-
“所有数字”有多少个数字?你尝试过什么,你在哪里卡住了?
-
50 个数字,我试图将所有数字(50)放在整个圆圈内,但不知何故数字只出现在左上角。如何解决这个错误?请解释...
-
@DavidThomas ,正如您在个人资料中提到的对新项目的兴趣,我的项目面临很多问题,任何帮助将不胜感激......(我不是在谈论这个问题..)
标签: javascript html jquery css arrays