Publi

Conocer la posición de un objeto (Javascript)

Posteo un par de funciones para saber la posición absoluta (x,y) de un objeto en Javascript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function getX( oElement )
{
  var iReturnValue = 0;
  while( oElement != null ) {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}

function getY( oElement )
{
  var iReturnValue = 0;
  while( oElement != null ) {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}

Encontré la función getY en este foro.

Con estas funciones podremos saber la posición de los objetos con respecto a la ventana del navegador.

También podría interesarte....

There are 2 comments left Ir a comentario

  1. Matias /
    Usando Google Chrome Google Chrome 29.0.1600.1 en Windows Windows 7

    Gracias, funciona perfecto.

  2. Top Gun Leather Jacket UK /
    Usando Google Chrome Google Chrome 116.0.0.0 en Windows Windows NT

    Excellent article. The writing style which you have used in this article is very good and it made the article of better quality.

Leave a Reply