我想确定区域被赋予任意数字的位置.
zones = [0, 150, 300, 400, 600, 800]
function checkZone(mouseX) {
// if mouseX is 321, it should be index 2 of zones array
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下循环来执行此操作.我已经包含了整个页面用于测试目的.
<html>
<body>
<script type="text/javascript">
var i; var y = 0; var val = 321; var zones = [0,150,300,400,600,800];
for (i = 0; i < zones.length; i++)
if (val >= zones[i])
y = i;
document.write("Value " + val + " is at position " + y);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
使用各种测试数据:
Value -99 is at position 0
Value 0 is at position 0
Value 149 is at position 0
Value 150 is at position 1
Value 321 is at position 2
Value 521 is at position 3
Value 799 is at position 4
Value 800 is at position 5
Value 999 is at position 5
Run Code Online (Sandbox Code Playgroud)