简体   繁体   English

“ if”语句参数未按预期运行。 JavaScript的

[英]“if” statement parameters not functioning as intended. JavaScript

The following JavaScript "if" statement is not reading the parameters as I intended both parameters to be read. 以下JavaScript “ if”语句未按我希望同时读取两个参数的方式读取参数。 When I shoot bulletOne with a function that uses an interval to run bulletOneLeft++; 当我使用带有使用间隔来运行bulletOneLeft ++的功能拍摄bulletOne the left CSS property of bulletOne increases by 1 with each interval as intended. bulletOne的左CSS属性按照预期的每个间隔增加1。 However, when I coded the following if statement to detect the CSS style.left property of the bulletOne and compare it to the CSS style.left property of an element called " targetBox "; 然而,当我编写if语句来检测bulletOne的CSS style.left财产,并将其与所谓的“targetBox”元素的CSS属性style.left以下; the code does not recognize the difference of the CSS style.left properties of these two elements. 代码无法识别这两个元素的CSS style.left属性的区别。 My question is how can I make this if statement execute once bulletOne's CSS style.left property is greater than targetBox's CSS style.left property numerically? 我的问题是,如何使BulletOne的CSS style.left属性在数值上大于targetBox的CSS style.left属性后执行if语句?

if (bulletOneLeft > targetLeftProp){
document.getElementById("targetBox").style.opacity = "0";   
};

The two parameters of bulletOneLeft and targetLeftProp are described with the following code; 以下代码描述了bulletOneLefttargetLeftProp的两个参数:

bulletOneLeft; bulletOneLeft;

#bulletOne {
position: absolute;
background-color: white;
height: 1px;
width: 6px;
top: 0px;
left: 0px;
}
bulletOne.style.left = bulletOneLeft;

targetLeftProp; targetLeftProp;

#targetBox{
position: absolute;
background-color: green;
height: 20px;
width: 20px;
top: 30px;
left: 310px;
opacity: 1;
}
var targetLeftProp = window.getComputedStyle(CSSelement, 
null).getPropertyValue("left");

PREVIOUS FAILED ATTEMPT; 先前失败的尝试; I have tried many different combinations of code, the following was my first failed attempt. 我尝试了许多不同的代码组合,以下是我第一次失败的尝试。 The following code seemed the most logical at the time; 以下代码在当时似乎是最合乎逻辑的;

var bulletLeft = document.getElementById("bulletOne").style.left;
var targetLeft = document.getElementById("targetBox").style.left;

if (bulletOneLeft > targetLeft){
   document.getElementById("targetBox").style.opacity = "0";   
};

So first of all you made a typo that is why it doesnt work in your failed attemps. 所以首先您做了错别字,这就是为什么它在失败的尝试中不起作用。

Your variable is named bulletOne but you do your if statement with bulletOneLeft . 您的变量名为bulletOne但是您可以使用bulletOneLeft执行if语句。

Also : 另外:

style.left;

return a string, so you have to do use parseFloat function or parseInt if you're sure it will always be an int ( https://www.w3schools.com/jsref/jsref_parsefloat.asp , https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt ) as following : 返回一个字符串,因此,如果您确定它始终是一个整数,则必须使用parseFloat函数或parseInt( https://www.w3schools.com/jsref/jsref_parsefloat.asp,https://developer.mozilla 。 org / en / docs / Web / JavaScript / Reference / Global_Objects / parseInt ),如下所示:

var bulletOne = parseFloat(document.getElementById("bulletOne").style.left;

And you are good to go I believe. 我相信,您走的很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM