M
M
MIsternik2014-05-31 12:02:17
JavaScript
MIsternik, 2014-05-31 12:02:17

Why can Javascript elementfrompoint return different elements?

There is a task, to find the nearest element along the Y axis to a certain point, using the string

$(document).ready(function(){
// что-то еще
var element = document.elementFromPoint(Math.floor(innerWidth/2), 500);
console.log("offset: {" + $(element).offset().top+ " : " + $(element).offset().left + "}");
// что-то еще
})


the point is at the bottom of the screen in the middle (this is not important).

I noticed that when I refresh the page, the elements get different.
I thought that the dot is between two adjacent elements, so...but no:

The page consists of text constructions of small width.

I randomly get elements with positions:
offset: {411 : 0}
offset: {483 : 0}
despite the fact that there are more elements between them, chrome shows the width as 60,40,20,20.

On what it can depend?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oscar Handsome, 2018-04-30
@oscarhandsome

It looks like it's here

if the page is scrolled, then the element will rise above or fall below - its coordinates relative to the window will change.

M
MIsternik, 2018-05-19
@MIsternik

The answer was actually simple. There was an image on the page that appeared on the page at a slightly different speed, not noticeable to the eye. And if it has not yet appeared, then I got on one element, and if it appeared on another. Perhaps this may be due to any other dynamic elements of the page. I'm not a frontend, so xs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question