<script>
let domList = document.querySelectorAll('.a');
let elementSize = 60; // 元素宽度和高度
let domTop = window.innerHeight;
let domLeft = window.innerWidth;
let gridRows = Math.floor(domTop / elementSize);
let gridCols = Math.floor(domLeft / elementSize);
console.log(gridRows,'gridRows',window.innerHeight);
let availablePositions = [];
for (let i = 0; i < gridRows; i++) {
for (let j = 0; j < gridCols; j++) {
availablePositions.push({ top: i * elementSize, left: j * elementSize });
}
}
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function aaa() {
shuffle(availablePositions);
domList.forEach((e, index) => {
if (index < availablePositions.length) {
e.style.top = availablePositions[index].top + 'px';
e.style.left = availablePositions[index].left + 'px';
}
});
}
setInterval(() => {
aaa();
}, 500);
</script>
屏幕的显示
最新推荐文章于 2024-07-24 16:20:17 发布