【问题标题】:Get non visible inner div获取不可见的内部 div
【发布时间】:2013-05-10 13:10:08
【问题描述】:

我有一个位置为 absolute 的 div(parentDivStyle),这是我的父 div。然后我在父 div 中有 5 个子 (childDivStyle) div,位置为 relative。我已将 overflow 设置为隐藏父 div。所以一些子 div 是不可见的。我想获取 jquery 不可见的 div。有什么办法吗?

我用谷歌搜索了它,大部分结果都与“可见”属性相关,这不是我想要的。而且我也不喜欢任何插件。请帮忙。

CSS

.parentDivStyle {
    overflow:hidden;
    width:300px;
    height:50px;
    position:absolute;
    background:#ccc;
    float:left;
}
.childDivStyle {
    width:100px;
    height:50px;
    position:relative;
    float:left;
    background:red;
    border: 1px solid black;
}

HTML

<div class="parentDivStyle">
<div class="childDivStyle">1</div>
<div class="childDivStyle">2</div>
<div class="childDivStyle">3</div>
<div class="childDivStyle">4</div>
<div class="childDivStyle">5</div>
</div>

JSFIDDLE

【问题讨论】:

  • 为什么要在父级上设置高度以及溢出:隐藏,如果您希望子级可见?不太清楚你在这里做什么。
  • @ralph.m,我正在构建一个滑块。我需要知道不可见的 div。
  • 你想显示所有不可见的.childDivStyle 还是一次只显示一个(想想精灵图像)?
  • 为什么不使用现成的 - 那里有数百万免费的
  • @MarcAudet 不,我只想识别不可见的

标签: javascript jquery css html


【解决方案1】:

您可以像这样使用子 div 的位置和父级的高度:

$('#parent .childDivStyle').each(function(index,div){
    if($(div).position().top > $('#parent').height()) alert($(div).html())
});

工作小提琴:http://jsfiddle.net/3suDz/3/

希望对你有帮助。

【讨论】:

  • 不错的答案,但我必须考虑它是向右对齐还是向左对齐。我正在添加 +1
  • 这不是万无一失的,因为它取决于布局的精确几何形状。例如,如果将黑色边框设置为 0px,则会得到不同的结果。然而,这个想法很好,但实现需要精确。
  • 我建议使用 outerWidth(true)outerHeight(true) 的组合,以便在尺寸计算中也包含填充、边框和边距。
  • 你可以使用 $(div).position().left, $(div).width() 和 $('#parent').width() 来测试左右对齐
【解决方案2】:

使用this answer 获取元素的坐标,您可以确定元素之间的相对位置。一旦知道了可见区域的坐标,就可以很容易地弄清楚哪些子元素是可见的。

这将告诉您元素是否可见,如果不可见,它们相对于容器的方向。

displayCoords = function(element) {
    var rect = element.getBoundingClientRect();
    console.log(rect.top, rect.right, rect.bottom, rect.left);   

    var childElements = element.children;
    for(i = 0; i < childElements.length; i++)
    {
        childRect = childElements[i].getBoundingClientRect();
        console.log(childRect.top, childRect.right, childRect.bottom, childRect.left);  
        if(childRect.top >=  rect.bottom)
            console.log("not visible -- off the bottom of element");
        else if(childRect.left >= rect.right)
            console.log("not visible -- off the right of element");
        else if(childRect.bottom <= rect.top)
            console.log("not visible -- off the top of element");
        else if(childRect.right <= rect.left)
            console.log("not visible -- off the left of element");
    }

}

我 fork 你的 JSFiddle here

【讨论】:

    【解决方案3】:

    试试下面的代码

    $('div.parentDivStyle div').each(function(index, element){
                alert(this.offsetTop + $(this).height() > $('div.parentDivStyle').height());
            }); 
    

    如果子 div 被隐藏,则返回 true,否则返回 false。

    检查小提琴http://jsfiddle.net/3suDz/4/

    【讨论】:

    • 这似乎运作良好。我调整了 HTML,例如,将边框设置为 0px,改变.parentDivStyle 的宽度,真/假值是准确的。这看起来相当健壮。请解释this.offsetTop,谢谢。
    • 它将返回当前子元素相对于父节点顶部的距离。 help.dottoro.com/ljnvukbb.php
    【解决方案4】:

    这是一个考虑到子 div 的相对性质的小提琴。它可以被压缩,但我把它留得很长,所以逻辑很明显

    http://jsfiddle.net/arpTx/18/

    $("#p").children().each(
            function(idx, el) { 
                var pos = $(el).position();
    
                console.log("child " + $(el).text() + " is visible: " + isVisible(pos.left, pos.top));
        });
    
    function isVisible(x, y) {
        var pos = $("#p").position();
        var left = pos.left;
        var right = left + $("#p").width();
        var top = pos.top;
        var bottom = top + $("#p").height();    
    
        x += left;
        y += top;
        return (x >= left && x < right) && (y >= top && y < bottom); }
    

    【讨论】:

      【解决方案5】:

      如何解决这个问题

      CSS

      .parentDivStyle {
          overflow:hidden;
          width:300px;
          height:50px;
          position:absolute;
          background:#ccc;
          float:left;
      }
      .childDivStyle {
          width:100px;
          height:50px;
          position:relative;
          float:left;
          background:red;
          border: 1px solid black;
      }
      

      HTML

      <div id="parent" class="parentDivStyle">
          <div class="childDivStyle">1</div>
          <div class="childDivStyle">2</div>
          <div class="childDivStyle">3</div>
          <div class="childDivStyle">4</div>
          <div class="childDivStyle">5</div>
      </div>
      

      Javascript

      function getNotVisible(parentId, childClassName) {
          var parent = document.getElementById(parentId),
              children,
              elements;
      
          if (parent) {
              children = parent.getElementsByClassName(childClassName);
              if (children) {
                  elements = [];
                  Array.prototype.forEach.call(children, function (child) {
                      var pBounds = parent.getBoundingClientRect(),
                          cBounds = child.getBoundingClientRect();
      
                      if (cBounds.right < pBounds.left || cBounds.left > pBounds.right || cBounds.bottom < pBounds.top || cBounds.top > pBounds.bottom) {
                          elements.push(child);
                      }
                  });
              }
          }
      
          return elements;
      }
      
      console.log(getNotVisible("parent", "childDivStyle"));
      

      开启jsfiddle

      顺便说一句,如果您想要一个 jquery 对象,请执行以下操作

      var $hiddens = $(getNotVisible("parent", "childDivStyle"));
      

      此外,如果您希望返回一个数组而不是未定义的数组,即如果父元素不存在或未找到子元素,则静默失败。

      然后删除

      elements = [];
      

      然后改变

      var parent = document.getElementById(parentId),
          children,
          elements = [];
      

      当然,这一切都取决于您正确设置 CSS,因为不会对 visibilityoverflow 等进行检查。

      如果您想添加 CSS 检查,以仔细检查您的 CSS 工作,那么您可以使用 window.getComputedStyle 并检查重要值。

      【讨论】:

        【解决方案6】:

        你可以使用 jQuery 的 is() 函数,像这样:

        $(element).is(":visible")
        

        所以在你的情况下,你会做这样的事情:

        var elems = $(".childDivStyle");
        for(var i = 0; i < elems.length; i++)
        {
            if(!($(elems[i]).is(":visible")))
            {
                // The element is hidden
            }
        }
        

        【讨论】:

        • 这并不像您预期​​的那样工作,因为所有.childDivStyle 元素都是可见的,这与由于溢出属性设置而被隐藏不同。见jsfiddle.net/audetwebdesign/3suDz/5
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        • 2014-09-06
        相关资源
        最近更新 更多