2

I am currently trying to figure out how to get the information of how much percent is visbile of a div inside a overflow-x:scroll container. I also need to know if it comes from the right or from the left ? Is this possible somehow ?

Sebastian Boldt
  • 5,283
  • 9
  • 52
  • 64
  • I don't think there is any mechanism in css or jquery. You will have to apply your own mathematics. You can find out height like this - `parent container height - content height`. – Ashwin Jun 12 '12 at 10:20

1 Answers1

0

This might be helpfull. You could edit the code from the answer to that question to check if the element was scrolled in from the right or the left.

Check if element is visible after scrolling

To calculate the percentage visable you just need to compare child's size to the parent's size and what the 'left' offset of the child is.

(I might add a code example later)

EDIT

I made a small example that show how you can detect the visible percentage of that child inside an "overflow-x:scroll" div-container and if the child comes from left or right.

<style>
#parent {
    overflow-x:scroll;
    width: 300px;
    height:120px;
    border: solid 1px #000;
}
#child {
    width: 200px;
    height: 100px;
    background:#FF0;
    margin-left: 200px;
}
#scrollPane {
    width: 800px;
}
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#button").click(function(){
        alert(percantageVisible() + "% of the child is visible");
    });
});

function percantageVisible()
{
    var parent = $("#parent");
    var parentLeftOffset = parent.offset().left;
    var parentRightOffset = parentLeftOffset + parent.width();

    var child = $("#child");
    var childLeftOffset = child.offset().left;
    var childRightOffset = childLeftOffset + child.width();

    if(childLeftOffset < parentLeftOffset && childRightOffset > parentLeftOffset && childRightOffset < parentRightOffset){
        // percentage from the left
        var width = child.width();
        var hiddenWidth = Math.abs(childLeftOffset - parentLeftOffset);
        var visibleWidth = width - hiddenWidth;
        return visibleWidth/(width/100);
    } 
    else if(childRightOffset > parentRightOffset && childLeftOffset < parentRightOffset && childLeftOffset > parentLeftOffset ){
        // percentage from the right
        var width = child.width();
        var hiddenWidth = Math.abs(parentRightOffset -childRightOffset);
        var visibleWidth = width - hiddenWidth;
        return visibleWidth/(width/100);
    }
    else if (childLeftOffset > parentLeftOffset && childRightOffset < parentRightOffset){
        // all visible
        return 100;
    }
    else{
        // invisible
        return 0;
    }
}
</script>

<div id="parent">
    <div id="scrollPane">
        <div id="child"> </div>
    </div>
</div>
<button id="button">check percentage</button>

Hope this helps

Community
  • 1
  • 1
akalucas
  • 476
  • 3
  • 13