当前位置:Gxlcms > JavaScript > Code:findPosX 和 findPosY

Code:findPosX 和 findPosY

时间:2021-07-01 10:21:17 帮助过:12人阅读

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

人气教程排行