大佬教程收集整理的这篇文章主要介绍了获得 10 分后需要重新加载页面,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
let click=0;
let gamescore=0;
function count(){
click++;
gamescore++;
document.getElementByID("mybutton").INNERHTML = click;
document.getElementByID('score').INNERHTML ="score: " + gamescore;
}
var div1=document.querySELEctor('div2');
var div2 = div1.children[0];
var d1=div1.getBoundingClIEntRect();
var d2=div2.getBoundingClIEntRect();
seTinterval((d=[Math.floor(Math.random()*(d1.wIDth-d2.wIDth)),Math.floor(Math.random()*(d1.height-d2.height))])=>{
div2.style.transform = `translate(${d[0]}px,${d[1]}pX)`;
},1000);
if (gamescore => 10){ alert("you wON!");
document.LOCATIOn.reload()};
clearInterval(interval);
body{
BACkground: violet;
}
#Contianer{
wIDth: 1000px;
height: 900px;
BACkground-color: darkred;
position: relative;
}
#GameBelt{
wIDth: 1000px;
height: 40px;
BACkground-color: black;
color: #4b3535;
position: relative;
border: 3px solID black;
}
#Playground{
wIDth: 1000px;
height:400px;
BACkground-color: rgb(15,86,167);
position: absolute;
border: 3px solID darkred;
}
#score{
left: 20%;
top: 50%;
BACkground: black;
color: red;
text-align: center;
padding: 5px;
outline:2px;
border: 2px solID red;
border-radius: 2px;
height: 20px;
wIDth: 65px;
Font-family: 'Franklin Gothic Medium','Arial Narrow',Arial,sans-serif;
}
button{
height: 40px;
wIDth: 40px;
}
#pasek{
wIDth:1000px;
height:50px;
BACkground-color:darkgreen;
position:relative;
}
#mIEJscegry{
wIDth:800px;
height:600px;
BACkground-color:darkblue;
position:absolute;
}
<div ID="Container">
<div ID="GameBelt">
<div ID="score">score:</div>
</div>
<div2 ID="Playground">
<button onclick="count()" ID="mybutton">0</button>
</div>
</div>
count
函数以复制您的警报代码
@H_772_2@我也知道您试图检查 gameScore
是否大于或等于 10 但 (gamescore) => 10
就像一个回调并且总是返回 true。这就是您在页面加载时收到 you wON
提醒的原因。
@H_772_2@LOCATIOn
对象在 window
上而不是在 document
上,因此不会对页面产生影响。
@H_772_2@不太重要的编辑被注释到代码中。试试游戏
@H_772_2@
let click = 0;
let gameScore = 0;
let interval;
// I want you to know div2 isn't a valid HTML tag,you could hvae just SELEcted the lement by it's id
const div1 = document.getElementById("Playground");
const div2 = document.getElementById("mybutton"); // you used this same SELEctor later in the count function
const d1 = div1.getBoundingClientRect();
const d2 = div2.getBoundingClientRect();
function count() {
click++;
gameScore++;
div2.innerHTML = click;
document.getElementById("score").innerHTML = "Score: " + gameScore;
// You need to check for the gameScore after every click inside this function
if (gameScore >= 10) {
alert("you wON!");
clearInterval(interval); // There's no real need to clear thE interval since the page will still be reloaded
LOCATIOn.reload();
}
}
interval = seTinterval((d = [Math.floor(Math.random() * (d1.width - d2.width)),Math.floor(Math.random() * (d1.height - d2.height))]) => div2.style.transform = `translate(${d[0]}px,${d[1]}pX)`,1000);
body {
width: 100%;
height: 100%;
BACkground: violet;
}
#Contianer {
width: 100%; /* I think the width and height are better to fit the screen rather than a random size for use experience*/
height: 100%;
BACkground-color: darkred;
position: relative;
}
#GameBelt {
width: 100%;
height: 40px;
BACkground-color: black;
color: #4b3535;
position: relative;
border: 3px solid black;
}
#Playground {
width: 100%;
height: calc(100% - 40pX); /*From the height of the GameBelt*/
BACkground-color: rgb(15,86,167);
position: absolute;
border: 3px solid darkred;
}
#score {
left: 20%;
top: 50%;
BACkground: black;
color: red;
text-align: center;
padding: 5px;
outline: 2px;
border: 2px solid red;
border-radius: 2px;
height: 20px;
width: 65px;
font-family: 'Franklin Gothic Medium','Arial Narrow',Arial,sans-serif;
}
button {
height: 40px;
width: 40px;
transition: transform .5s ease; /* I added this transition for smooth movements */
}
<div id="Container">
<div id="GameBelt">
<div id="score">Score:</div>
</div>
<div id="Playground">
<button onclick="count()" id="mybutton">0</button>
</div>
</div>
let click = 0;
let gameScore = 0;
function count() {
click++;
gameScore++;
document.getElementById("mybutton").innerHTML = click;
document.getElementById('score').innerHTML = "Score: " + gameScore;
if (gameScore >= 10) {
alert("you wON!");
window.LOCATIOn.href = window.LOCATIOn.href;
clearInterval(interval);
}
}
var div1 = document.querySELEctor('div2');
var div2 = div1.children[0];
var d1 = div1.getBoundingClientRect();
var d2 = div2.getBoundingClientRect();
var interval = seTinterval((d = [Math.floor(Math.random() * (d1.width - d2.width)),Math.floor(Math.random() * (d1.height - d2.height))]) => {
div2.style.transform = `translate(${d[0]}px,${d[1]}pX)`;
},1000);
body {
BACkground: violet;
}
#Contianer {
width: 1000px;
height: 900px;
BACkground-color: darkred;
position: relative;
}
#GameBelt {
width: 1000px;
height: 40px;
BACkground-color: black;
color: #4b3535;
position: relative;
border: 3px solid black;
}
#Playground {
width: 1000px;
height: 400px;
BACkground-color: rgb(15,sans-serif;
}
button {
height: 40px;
width: 40px;
}
#pasek {
width: 1000px;
height: 50px;
BACkground-color: darkgreen;
position: relative;
}
#miejscegry {
width: 800px;
height: 600px;
BACkground-color: darkblue;
position: absolute;
}
<div id="Container">
<div id="GameBelt">
<div id="score">Score:</div>
</div>
<div id="Playground">
<button onclick="count()" id="mybutton">0</button>
</div>
</div>
if
语句。首先,它需要是 gameScore >= 10
,而不是 gameScore => 10
。 =>
箭头通常为 signifies a function,而不是大于或等于。接下来,if
语句只执行一次。每次运行该函数时都需要执行它,因此它需要在 count
函数中。
@H_772_2@您遇到的第二个错误是您的 clearInterval
。 clearInterval
函数需要一个输入间隔来清除它,而您刚刚声明了一个未定义的变量 interval
。我只是通过将 var interval
设置为您所写的间隔来定义它:
var interval = seTinterval((d=[Math.floor(Math.random()*(d1.width-d2.width)),Math.floor(Math.random()*(d1.height-d2.height))])=>{
div2.style.transform = `translate(${d[0]}px,1000);
@H_772_2@最后,您的 window.LOCATIOn.reload
在 codepen 中有点挑剔,所以我只是将其更改为 window.LOCATIOn.href = window.LOCATIOn.href
,它只是完全重新加载页面。以上是大佬教程为你收集整理的获得 10 分后需要重新加载页面全部内容,希望文章能够帮你解决获得 10 分后需要重新加载页面所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。