Javascript 如何按特定顺序推送图像坐标?
我可以选择图像坐标。但我想在数组中按特定顺序选择或排列单击的图像坐标,即:Javascript 如何按特定顺序推送图像坐标?,javascript,html,jquery,Javascript,Html,Jquery,我可以选择图像坐标。但我想在数组中按特定顺序选择或排列单击的图像坐标,即: [ "top-left" , "top-right" , "bottom-left" , "bottom-right" ] 代码: 您可以执行以下操作并确定象限: var coordinates = new Array(4); //<--fixed size var counter = 0 var width = 200; var
[ "top-left" , "top-right" , "bottom-left" , "bottom-right" ]
代码:
您可以执行以下操作并确定象限:
var coordinates = new Array(4); //<--fixed size
var counter = 0
var width = 200;
var height = 200;
$(document).ready(function() {
$("img").on("click", function(event) {
if(counter<4){
var x = event.pageX - this.offsetLeft;
var y = event.pageY - this.offsetTop;
if(x < width/2) {
if(y < height/2) {
coordinates[0] = [x,y];
//console.log("top-left"); //<---first quadrant
} else {
coordinates[2] = [x,y];
//console.log("bottom-left"); //<---third quadrant
}
} else {
if(y < height/2) {
coordinates[1] = [x,y];
//console.log("top-right"); //<---second quadrant
} else {
coordinates[3] = [x,y];
//console.log("bottom-right"); //<---fourth quadrant
}
}
console.log(coordinates)
counter++;
}
});
});
var坐标=新数组(4)// 您可以执行以下操作并确定象限:
var coordinates = new Array(4); //<--fixed size
var counter = 0
var width = 200;
var height = 200;
$(document).ready(function() {
$("img").on("click", function(event) {
if(counter<4){
var x = event.pageX - this.offsetLeft;
var y = event.pageY - this.offsetTop;
if(x < width/2) {
if(y < height/2) {
coordinates[0] = [x,y];
//console.log("top-left"); //<---first quadrant
} else {
coordinates[2] = [x,y];
//console.log("bottom-left"); //<---third quadrant
}
} else {
if(y < height/2) {
coordinates[1] = [x,y];
//console.log("top-right"); //<---second quadrant
} else {
coordinates[3] = [x,y];
//console.log("bottom-right"); //<---fourth quadrant
}
}
console.log(coordinates)
counter++;
}
});
});
var坐标=新数组(4)//鉴于您的形象,您能否提供预期的输出结果?其中一个总是0,0吗?或者this.offsetLeft/this.offsetTop?给定的代码有什么问题?有什么不起作用的吗?鉴于你的形象,你能提供预期的输出吗?其中一个总是0,0吗?或者this.offsetLeft/this.offsetTop?给定的代码有什么问题?有什么不起作用的吗?谢谢你的回复。您的解决方案在JSFIDLE上工作,但在我的脚本上不工作。如果我选择左上角,然后坐标添加到左下角数组,然后选择右上角,然后坐标添加到右下角数组,请详细说明“不处理我的脚本?”。随附的解决方案给出了一个想法,即如何根据所问问题实现您的目标。你能用你正在做的事情更新问题描述吗?谢谢你的回答。您的解决方案在JSFIDLE上工作,但在我的脚本上不工作。如果我选择左上角,然后坐标添加到左下角数组,然后选择右上角,然后坐标添加到右下角数组,请详细说明“不处理我的脚本?”。随附的解决方案给出了一个想法,即如何根据所问问题实现您的目标。你能用你正在做的事情更新问题描述吗?
http://jsfiddle.net/t03nducb/
var coordinates = new Array(4); //<--fixed size
var counter = 0
var width = 200;
var height = 200;
$(document).ready(function() {
$("img").on("click", function(event) {
if(counter<4){
var x = event.pageX - this.offsetLeft;
var y = event.pageY - this.offsetTop;
if(x < width/2) {
if(y < height/2) {
coordinates[0] = [x,y];
//console.log("top-left"); //<---first quadrant
} else {
coordinates[2] = [x,y];
//console.log("bottom-left"); //<---third quadrant
}
} else {
if(y < height/2) {
coordinates[1] = [x,y];
//console.log("top-right"); //<---second quadrant
} else {
coordinates[3] = [x,y];
//console.log("bottom-right"); //<---fourth quadrant
}
}
console.log(coordinates)
counter++;
}
});
});