javascript - Can't call the window position object in a function
问题描述
I have the following function that works like a champ:
//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
};
}
so when I call the getPosition I get the numbers as an object like that:
>{x: 200, y: 500}
Now I have another function that I am building to trigger an event with a certain position, but I can't call the getPosition number right... I have tried dozens of variations, certainly I am missing something here. Below is a simple function to show the way I am calling the position number is not correct:
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!')
}
};
I have also tried between other options this:
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!')
}
};
and this:
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!')
}
};
So I only get 'it is not there!', I have tried many different ways to find a way to compare without success.
Any idea what I am doing wrong?
Much appreciated.
解决方案
该getPosition()
函数返回一个对象。因此,您必须检查对象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=""/>
推荐阅读
- c++ - Qt + conan = 使用空输出设备,无可用
- php - 删除换行符的问题
- object-detection-api - 如何删除检测分数(百分比)?
- android - 无法在 TextEdit 或任何其他编辑字段中键入希伯来字母
- symfony - Symfony Sonata Admin:如何从 DB 中获取选择数组
- c++ - 调用POCO c ++ ServerSocket()时Linux上的分段错误
- javascript - OnClick 可以做两件事吗?
- java - 无法使用 BufferedWriter 将双精度写入文件
- electron - electron toggleDevTools() 方法未定义
- json - 在 T-SQL 中构造嵌套的 json 数组