zl程序教程

您现在的位置是:首页 >  前端

当前栏目

Code:findPosX和findPosY

2023-06-13 09:13:47 时间
/**
 * 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;
}