Home > Web Front-end > JS Tutorial > Code: findPosX and findPosY_Basics

Code: findPosX and findPosY_Basics

WBOY
Release: 2016-05-16 19:23:13
Original
1478 people have browsed it

/**
 * Find the X position of an object, relative to the viewport
 * Code copied from quirksmode.org
 * @param obj Object to find x position for
 */
function findPosX(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curleft  = obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft  = obj.x;
  return curleft;
}

/**
 * Find the Y position of an object, relative to the viewport
 * Code copied from quirksmode.org
 * @param obj Object to find y position for
 */
function findPosY(obj)
{
  var curtop = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curtop  = obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop  = obj.y;
  return curtop;
}

Related labels:
and
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template