简体   繁体   English

是否可以在“ overflow-x:scroll” div容器内证明div元素的可见性?

[英]Is it possible to proof an div element of visibility inside a “overflow-x:scroll” div-container ?

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. 我目前正在试图弄清楚如何获取溢出x:scroll容器中div的可见性百分比的信息。 I also need to know if it comes from the right or from the left ? 我还需要知道它是来自右侧还是左侧? Is this possible somehow ? 这有可能吗?

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. 我做了一个小例子,展示了如何在“ overflow-x:scroll” div容器内检测该孩子的可见百分比, 以及该孩子来自左还是右。

<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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM