简体   繁体   English

如何确定绝对定位元素的容器

[英]How to determine an absolute-positioned element's container

I have a custom control that needs to render a pop-up window directly below its "main" control body (a DIV element). 我有一个自定义控件,需要在其“主”控件主体(DIV元素)正下方呈现一个弹出窗口。 The problem I'm running into is how to set the pop-up coordinate location if the control has no "knowledge" of the its container settings. 我遇到的问题是,如果控件对其容器设置没有“知识”,那么如何设置弹出坐标位置。

For example, take this section of code: 例如,使用以下代码段:

// library call to extract document-based coordinates (returns object with X and Y fields) of the "controlBodyElement" (a DIV)
var pt = some.library.getDocumentPosition(controlBodyElement)
// frame (the "popup") is a DIV pointer 
frame.style.position = "absolute"
frame.style.top = pt.y + controlBodyElement.clientHeight + 1 + "px"  // Line "A"
// frame.style.top = "0px"   // Line "B" -- finds the relativity point of absolute-position 

Line "A" - causes the popup to be rendered way below the controlBodyElement 线“ A”-使弹出窗口呈现在controlBodyElement下面
Line "B" - renders the popup way above the controlBodyElement. 线“ B”-在controlBodyElement上方呈现弹出方式。

Q: what element setting/attribute should be searched for in the DOM tree to determine which element some absolute-positioned child is being anchored relative to? 问:应该在DOM树中搜索哪个元素设置/属性,以确定某个绝对位置的子元素相对于哪个元素锚定?

UPDATE: I think if someone can explain to me what page mechanics would cause an absolute-positioned element (using top = 0px) to be rendered halfway down the page (instead of the top) then I can write the logic to sort things out; 更新:我认为,如果有人可以向我解释什么页面机制会导致绝对定位的元素(使用top = 0px)呈现在页面的一半下方(而不是顶部),那么我可以编写逻辑来整理内容; I'm just not sure I need to be looking for... 我只是不确定我是否需要寻找...

Thanks to Pumbaa80 for the information - that was exactly what I was trying to figure out. 感谢Pumbaa80提供的信息-这正是我想要弄清楚的。

In case it'll help someone else later, here is a revamped locator method that will extract that particular offset coordinates (as opposed to a logical screen location)... 如果以后会帮助别人,这里有一个改进的定位器方法,它将提取特定的偏移坐标(与逻辑屏幕位置相对)...

// relative location from nearest Positioned ancestor
getPositionedOffset = function(element, coordinates) {
  // create a coordinates object if one was not assigned (first iteration)
  if(coordinates == undefined) { 
    coordinates = {x: 0, y: 0 }
  }

  if (element.offsetParent) {
    switch(window.getComputedStyle(element).position) {
      case "relative":
      case "absolute":
      case "fixed":
        return coordinates
      default:
        coordinates.x += element.offsetLeft
        coordinates.y += element.offsetTop
        getPositionedOffset(element.offsetParent, coordinates) // step into offsetParent
    }
  }
  return coordinates
}

NOTE: code is functional in Chrome; 注意:代码可在Chrome浏览器中使用; minor tweaking will be needed for operation in some of the other browser flavors. 在其他一些浏览器版本中,将需要进行细微调整。

EDIT: 编辑:

In most cases the function will be called with a single argument (that being an element reference), like so: 在大多数情况下,将使用单个参数(即元素引用)调用该函数,如下所示:

var ele = document.getElementById("foo")
var relativeLoc = getPositionedOffset(ele)

However, if a manual shift needs to be factored in (for example +5px right, and -10px up) then include the second argument: 但是,如果需要考虑手动换档(例如,向右+ 5px,向上-10px),则添加第二个参数:

var relativeLocWithOffset = getPositionedOffset(ele, {x:5, y:-10})  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM