Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get non visible inner div

I have a div(parentDivStyle) with position absolute which is my parent div. Then I have 5 children(childDivStyle) div inside the parent div with position relative. I have set the overflow to hidden of the parent div. So some of the child divs are not visible. I would like to get the divs which are not visible by jquery. Is there any way?

I have googled it and most of the results where related to "visible" property, That is not what I want. And also I am not preferring any plugin. Any help please.

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

like image 777
arjuncc Avatar asked May 10 '13 13:05

arjuncc


People also ask

How to hide overflow content CSS?

A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. The white-space property must be set to nowrap and the overflow property must be set to hidden. The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string.

How to fix overflowing DIV?

Add a padding top on the #scrollable div and make the #menu div absolute. Add a z-index to the #menu div so it stays on top. Also, make sure to add box-sizing: border-box on all elements to prevent exceeding the assigned sizes. Save this answer.

Why is my content overflowing?

Overflow happens when there is too much content to fit in a box. CSS provides various tools to manage overflow. As you go further with CSS layout and writing CSS, you will encounter more overflow situations.


2 Answers

You can use the position of the child divs, and the height of the parent like this:

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

The working fiddle: http://jsfiddle.net/3suDz/3/

Hope it helps.

like image 62
conca Avatar answered Sep 30 '22 15:09

conca


Using this answer about getting coordinates of elements, you can figure out where elements are in respect to each other. Once you know the coordinates of the visible area, you can easily figure out what child elements are visible.

This will tell you whether the elements are visible, and if not, which direction they are with respects to the container.

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");
    }

}

I forked your JSFiddle here

like image 39
Indigenuity Avatar answered Sep 30 '22 16:09

Indigenuity