无法在函数中调用 window 位置对象
Can't call the window position object in a function
我有以下功能,效果非常好:
//To find the position of an element
function getPosition(el) {
var xPos = 0;
var yPos = 0;
while (el) {
if (el.tagName == "BODY") {
// deal with browser quirks with body/window/document and page scroll
var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
var yScroll = el.scrollTop || document.documentElement.scrollTop;
xPos += (el.offsetLeft - xScroll + el.clientLeft);
yPos += (el.offsetTop - yScroll + el.clientTop);
} else {
// for all other non-BODY elements
xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPos += (el.offsetTop - el.scrollTop + el.clientTop);
}
el = el.offsetParent;
}
return {
x: xPos,
y: yPos
};
}
所以当我调用 getPosition 时,我将数字作为一个对象获取:
>{x: 200, y: 500}
现在我正在构建另一个函数来触发具有特定位置的事件,但我无法正确调用 getPosition 编号...我已经尝试了数十种变体,当然我在这里遗漏了一些东西。
下面是一个简单的函数来显示我调用位置号码的方式不正确:
function test () {
console.log(getPosition(img));//An image - getPosition identify it to be at {x:200,y:500}
if (getPosition(img) == (200,500)){
console.log('It is there!')
} else {
console.log('it is not there!')
}
};
我也尝试过其他选项:
function test () {
console.log(getPosition(img));
var x = 200;
var y = 500;
if (getPosition(img) == {x,y}){
console.log('It is there!')
} else {
console.log('it is not there!')
}
};
还有这个:
function test () {
console.log(getPosition(img));
if (getPosition(img) == {x: 200,y: 500}){
console.log('It is there!')
} else {
console.log('it is not there!')
}
};
所以我只得到'it is not there!',我尝试了很多不同的方法来寻找比较的方法都没有成功。
知道我做错了什么吗?
非常感谢。
getPosition()
函数returns一个对象。因此,您必须检查对象 x
和 y
属性以确保它们符合您的要求。
最简单的方法是在 if
语句中单独检查 x 和 y。这似乎是您想要做的,但您只是通过尝试在 1 个语句中检查所有 x 和 y 属性来使语法有点偏离。
//To find the position of an element
function getPosition(el) {
var xPos = 0;
var yPos = 0;
while (el) {
if (el.tagName == "BODY") {
// deal with browser quirks with body/window/document and page scroll
var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
var yScroll = el.scrollTop || document.documentElement.scrollTop;
xPos += (el.offsetLeft - xScroll + el.clientLeft);
yPos += (el.offsetTop - yScroll + el.clientTop);
} else {
// for all other non-BODY elements
xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPos += (el.offsetTop - el.scrollTop + el.clientTop);
}
el = el.offsetParent;
}
return {
x: xPos,
y: yPos
};
}
test()
function test() {
var img = document.getElementById("the-image");
console.log(getPosition(img));
var imageLocation = getPosition(img);
if (imageLocation.x === 8 && imageLocation.y === 22) {
console.log('It is there!')
} else {
console.log('it is not there!')
}
};
<img id="the-image" src=""/>
我有以下功能,效果非常好:
//To find the position of an element
function getPosition(el) {
var xPos = 0;
var yPos = 0;
while (el) {
if (el.tagName == "BODY") {
// deal with browser quirks with body/window/document and page scroll
var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
var yScroll = el.scrollTop || document.documentElement.scrollTop;
xPos += (el.offsetLeft - xScroll + el.clientLeft);
yPos += (el.offsetTop - yScroll + el.clientTop);
} else {
// for all other non-BODY elements
xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPos += (el.offsetTop - el.scrollTop + el.clientTop);
}
el = el.offsetParent;
}
return {
x: xPos,
y: yPos
};
}
所以当我调用 getPosition 时,我将数字作为一个对象获取:
>{x: 200, y: 500}
现在我正在构建另一个函数来触发具有特定位置的事件,但我无法正确调用 getPosition 编号...我已经尝试了数十种变体,当然我在这里遗漏了一些东西。 下面是一个简单的函数来显示我调用位置号码的方式不正确:
function test () {
console.log(getPosition(img));//An image - getPosition identify it to be at {x:200,y:500}
if (getPosition(img) == (200,500)){
console.log('It is there!')
} else {
console.log('it is not there!')
}
};
我也尝试过其他选项:
function test () {
console.log(getPosition(img));
var x = 200;
var y = 500;
if (getPosition(img) == {x,y}){
console.log('It is there!')
} else {
console.log('it is not there!')
}
};
还有这个:
function test () {
console.log(getPosition(img));
if (getPosition(img) == {x: 200,y: 500}){
console.log('It is there!')
} else {
console.log('it is not there!')
}
};
所以我只得到'it is not there!',我尝试了很多不同的方法来寻找比较的方法都没有成功。
知道我做错了什么吗?
非常感谢。
getPosition()
函数returns一个对象。因此,您必须检查对象 x
和 y
属性以确保它们符合您的要求。
最简单的方法是在 if
语句中单独检查 x 和 y。这似乎是您想要做的,但您只是通过尝试在 1 个语句中检查所有 x 和 y 属性来使语法有点偏离。
//To find the position of an element
function getPosition(el) {
var xPos = 0;
var yPos = 0;
while (el) {
if (el.tagName == "BODY") {
// deal with browser quirks with body/window/document and page scroll
var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
var yScroll = el.scrollTop || document.documentElement.scrollTop;
xPos += (el.offsetLeft - xScroll + el.clientLeft);
yPos += (el.offsetTop - yScroll + el.clientTop);
} else {
// for all other non-BODY elements
xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPos += (el.offsetTop - el.scrollTop + el.clientTop);
}
el = el.offsetParent;
}
return {
x: xPos,
y: yPos
};
}
test()
function test() {
var img = document.getElementById("the-image");
console.log(getPosition(img));
var imageLocation = getPosition(img);
if (imageLocation.x === 8 && imageLocation.y === 22) {
console.log('It is there!')
} else {
console.log('it is not there!')
}
};
<img id="the-image" src=""/>