【问题标题】:is there any way we can get positioned ancestor for an absolute positioned element有什么方法可以为绝对定位元素获取定位祖先
【发布时间】:2017-09-19 22:33:07
【问题描述】:

我们如何知道离绝对元素最近的定位父元素? 任何可玩的 js 脚本或插件

【问题讨论】:

  • [重复] 您可以在这里找到答案:Is there a CSS parent selector?
  • 要求我们建议、查找或推荐书籍、工具、软件库、插件、教程、解释技术或提供任何其他场外资源的问题对于 Stack Overflow Stack 来说是题外话溢出

标签: html css css-position absolute


【解决方案1】:

我会开始玩这样的东西:

function getPositionAnchor(startFromElm) {
  var positionAnchor = null;
  var validAnchors = ['relative','absolute','fixed'];
  var elm = startFromElm.parentElement;    
  do {
    if (validAnchors.indexOf(getComputedStyle(elm)['position']) !== -1
        || elm.tagName.toLowerCase() === 'html') {
      positionAnchor = elm;
    } else {
      elm = elm.parentElement;  
    }
  } while (!positionAnchor)
  return positionAnchor;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2018-01-28
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多