我做了一个基本的javascript代码,这样你就可以用鼠标戳divs了。
不幸的是,我必须手动添加它们,但我想添加这么多的模式。首先,我决定使用网格,但我猜它不会工作,因为div(我称之为正方形从现在开始:d)可以改变它们的位置。
所以我想问,我怎样才能创建一个javascript代码,我可以产生他们,直到他们填补了屏幕。
我还有另一个问题,这是realted这个项目,我怎么能使这些广场只是装饰。我的意思是装饰我不希望他们影响网站在所有,当块走出屏幕的身体开始消耗,有什么办法可以避免?
(Also如果你把片段全屏显示会更好!)
speedX = 0
speedY = 0
square = document.getElementsByClassName("square")
document.onmousemove=function(e){
speedX = e.movementX;
speedY = e.movementY;
posX = e.pageX;
posY = e.pageY;
}
setInterval(() => {
movement = Math.sqrt(speedX*speedX + speedY*speedY)
speed = movement * 10
}, 100);
function AddSquare(){
const newDiv = document.createElement("div")
const parent = document.querySelector(".background")
newDiv.classList.add("square")
parent.appendChild(newDiv)
}
for (let i = 0; i < square.length; i++) {
const element = square[i];
element.style.backgroundColor = element.getAttribute("color")
element.addEventListener('mouseover',() => {
const y = speedY
const x = speedX
const rad = Math.atan2(y, x)
yAxis = movement * Math.sin(rad)
xAxis = movement * Math.cos(rad)
element.style.left = `${Math.round(element.getBoundingClientRect().x + xAxis * 3)}px`
element.style.top = `${Math.round(element.getBoundingClientRect().y + yAxis * 3)}px`
element.style.transform = `rotate(${rad * (180/Math.PI)}deg)`
});
}
body{
margin: 0;
width: 100vw;
height: 100vh;
display: flex;
}
.square{
background-color: lightcoral;
width: 75px;
height: 75px;
position: absolute;
transform: rotate(0deg);
transition: all ease-out 0.5s;
}
#Header{
font-size: italic;
}
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="css.css">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="background">
<div class="square" style="top: 150px; left: -20px;" color="lightcoral"></div>
<div class="square" style="top: 150px; left: 80px;" color="bisque"></div>
<div class="square" style="top: 150px; left: 180px;" color="lightcoral"></div>
<div class="square" style="top: 150px; left: 280px;" color="aquamarine"></div>
<div class="square" style="top: 150px; left: 380px;" color="bisque"></div>
<div class="square" style="top: 150px; left: 480px;" color="lightcoral"></div>
<div class="square" style="top: 150px; left: 580px;" color="aquamarine"></div>
<div class="square" style="top: 150px; left: 680px;" color="bisque"></div>
<div class="square" style="top: 150px; left: 780px;" color="lightcoral"></div>
<div class="square" style="top: 150px; left: 880px;" color="aquamarine"></div>
<div class="square" style="top: 150px; left: 980px;" color="bisque"></div>
<div class="square" style="top: 150px; left: 1080px;" color="lightcoral"></div>
<div class="square" style="top: 250px; left: -30px;" color="cadetblue"></div>
<div class="square" style="top: 250px; left: 70px;" color="cadetblue"></div>
<div class="square" style="top: 250px; left: 170px;" color="greenyellow"></div>
<div class="square" style="top: 250px; left: 270px;" color="yellowgreen"></div>
<div class="square" style="top: 250px; left: 370px;" color="cadetblue"></div>
<div class="square" style="top: 250px; left: 470px;" color="greenyellow"></div>
<div class="square" style="top: 250px; left: 570px;" color="yellowgreen"></div>
<div class="square" style="top: 250px; left: 670px;" color="cadetblue"></div>
<div class="square" style="top: 250px; left: 770px;" color="greenyellow"></div>
<div class="square" style="top: 250px; left: 870px;" color="yellowgreen"></div>
<div class="square" style="top: 250px; left: 970px;" color="cadetblue"></div>
<div class="square" style="top: 350px; left: -40px;" color="lightcoral"></div>
<div class="square" style="top: 350px; left: 60px;" color="bisque"></div>
<div class="square" style="top: 350px; left: 160px;" color="lightcoral"></div>
<div class="square" style="top: 350px; left: 260px;" color="aquamarine"></div>
<div class="square" style="top: 350px; left: 360px;" color="bisque"></div>
<div class="square" style="top: 350px; left: 460px;" color="lightcoral"></div>
<div class="square" style="top: 350px; left: 560px;" color="aquamarine"></div>
<div class="square" style="top: 350px; left: 660px;" color="bisque"></div>
<div class="square" style="top: 350px; left: 760px;" color="lightcoral"></div>
<div class="square" style="top: 350px; left: 860px;" color="aquamarine"></div>
<div class="square" style="top: 350px; left: 960px;" color="bisque"></div>
</div>
Work in progress...
<div class="container text-center">
<div class="row">
<div class="col"><h1 id="Header">Work in progress...</h1></div>
</div>
</div>
<script type="text/javascript" src = "javascript.js"></script>
</body>
</html>
1条答案
按热度按时间k97glaaz1#
你可能有一个函数,给定一个容器,将填充多少方块可以适合只要有可用的宽度和高度的目标。
在这个演示中,我更好地分解了你的代码,并添加了一个名为
drawSquares
的主函数,当按钮reDRAW
被单击时,它会被调用。我使用一个按钮来触发方框绘制,因为可用空间取决于触发操作时区域的大小。例如,您可以展开代码片段,然后决定重新绘制方框以再次填充整个新区域。
您可以决定在文档就绪或窗口大小调整时调用该操作。