JavaScript-如何制作它,以便我可以重复该功能?
每次点击按钮时,这里的代码都应该在网页中随机位置移动块,但是我编写的功能仅在单击按钮时将块移动一次。我正在尝试做到这一点,以便每次点击按钮时,块都会移动。我应该怎么办?
/*-- dont mind the css, I think nothings wrong here, maybe? --*/
body{
height: 100vh;
width: 100%;
margin: 0;
padding: 0;
box-sizing: border-box;
}
button{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box{
height: 170px;
width: 170px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgb(190, 46, 46);
transition: 0.5s;
}
<body>
<div class="box" id="box"></div>
<!-- I tried onclick and onmousedown functions, but they dont work-->
<button type="button" onmousedown="activationButton()">Click Me</button>
<script>
const box = document.getElementById("box");
const button = document.getElementById("activationButton");
const transformX = Math.floor(Math.random() * 300);
const transformY = Math.floor(Math.random() * 330);
const positionXY = Math.floor(Math.random() * 90);
// function doesn't repeat itself. I think it is because the variables transformX and
//transformY
// doesn't reset, thus it locks the block in place?
function activationButton() {
box.style.transform = `translate( -${transformX}%, -${transformY}%)`;
box.style.right = `${positionXY}%`
box.style.left = `${positionXY}%`
}
</script>
</body>
the code here is supposed to move the block in a random position in the webpage every time the button is clicked, but the function I wrote only moves the block once when the button is clicked. I am trying to make it so that every time the button is clicked, the block moves. What should I do?
/*-- dont mind the css, I think nothings wrong here, maybe? --*/
body{
height: 100vh;
width: 100%;
margin: 0;
padding: 0;
box-sizing: border-box;
}
button{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box{
height: 170px;
width: 170px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgb(190, 46, 46);
transition: 0.5s;
}
<body>
<div class="box" id="box"></div>
<!-- I tried onclick and onmousedown functions, but they dont work-->
<button type="button" onmousedown="activationButton()">Click Me</button>
<script>
const box = document.getElementById("box");
const button = document.getElementById("activationButton");
const transformX = Math.floor(Math.random() * 300);
const transformY = Math.floor(Math.random() * 330);
const positionXY = Math.floor(Math.random() * 90);
// function doesn't repeat itself. I think it is because the variables transformX and
//transformY
// doesn't reset, thus it locks the block in place?
function activationButton() {
box.style.transform = `translate( -${transformX}%, -${transformY}%)`;
box.style.right = `${positionXY}%`
box.style.left = `${positionXY}%`
}
</script>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的随机位置只会声明一次。将其移入功能中,每次点击时随机对其进行随机化
Your random position only gets declared once. Move it inside of the function to randomize it everytime it gets clicked
您正在计算随机
transformx
和transformy
一次,并且每次在activateButton
函数中使用相同的值。您需要将随机化放入功能中。另外,我将
onMousedown
更改为onclick
。You are calculating random
transformX
andtransformY
only once and using the same value every time inside theactivateButton
function.You need to put the randomisation inside the function. Also I changed
onmousedown
toonclick
.