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

Code: findPosX and findPosY_Basics

WBOY
WBOYOriginal
2016-05-16 19:23:131481browse

/**
 * 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;
}

Statement:
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