2012-11-19 94 views
1

Illustration隐藏超出外部div高度的内部div(s)

应该隐藏整个元素。外部div上不应该有滚动条。这可以用CSS来实现还是需要jQuery?这如何实现?

+2

你能显示你的代码吗? –

+1

你可以尝试“overflow:hidden”,但我认为这只会隐藏div高于height的部分(而不是整个div)。 – Igor

+0

你想隐藏整个元素吗?或仍然显示在外部界限内的部分? – ryadavilli

回答

3

的总体思路是:

$("div div").filter(function() { 
    var $this = $(this), 
     pTop = $this.parent().offset().top, // parent position 
               // (no need if parent has 
               // "position: relative") 

     pHeight = $this.parent().height(),  // parent inner height 

     eTop = $this.offset().top,    // block position 
               // (can be replaced with 
               // "$this.position().top" 
               // if parent has 
               // "position: relative") 

     eHeight = $this.outerHeight(true);  // block outer height 

    return (eTop + eHeight) > (pTop + pHeight); 
}).hide(); 

(理论上,这应该工作


另一种方法:

var sumHeight = 0; 
$("div div").filter(function() { 
    var $this = $(this), 
     pHeight = $this.parent().height();  // parent inner height 

    sumHeight += $this.outerHeight(true);  // + block outer height 

    return sumHeight > pHeight; 
}).hide(); 
+0

假设外部div是名称“mainLeft”和内部div被命名为“行”,如果它然后是$(“mainLeft行”)? – mupersan82

+0

@ user1750323不,我不选择两个'div's。我只是表明我在'div'中选择'div',即在一个容器内的块。你可以用'$(“#容器.block”)或其他来代替它。 – VisioN

+0

好吧,明白了。谢谢。 – mupersan82

1

overflow:hidden;属性添加到外部div。

+3

这将削减最低的部分隐藏它只是部分 –

2

这不是在测试所有的,很可能需要调整,但给你一个大致的想法如何用jQuery做到这一点:

var container = $('#container'); 
var element = $('#element'); 

if ((element.position().top + element.position.height()) > container.height()) { 
    element.hide(); 
}