Determine visibility / real z-index of html elements

前端 未结 3 1794
孤独总比滥情好
孤独总比滥情好 2020-12-21 03:21

Is it possible to determine if an html element is visible to the user?

Example

A page has an input field with a datepicker. If the user clicks on the input

相关标签:
3条回答
  • 2020-12-21 03:28

    I tried a different approach using elements coordinates (getBoundingClientRect) and then using elementFromPoint to see if the element is hidden or visible.

    DEMO (Follow the instruction on the right side)

            var rectPos = this.getBoundingClientRect();
    
            var result = 0;
            if (this == document.elementFromPoint(rectPos.left, 
                                                        rectPos.top)) {
                result++;
            }
            if (this == document.elementFromPoint(rectPos.left, 
                                                        rectPos.bottom - 1)) {
                result++;
            }
            if (this == document.elementFromPoint(rectPos.right - 1, 
                                                         rectPos.top)) {
                result++;
            }
            if (this == document.elementFromPoint(rectPos.right - 1, rectPos.bottom - 1)) {
                result++;
            }
    
            if (result == 4) {
                result = 'visible';
            } else if (result == 0) {
                result = 'hidden';
            } else {
                result = 'partially visible';
            }
    

    Further Readings: getBoundingClientRect, elementFromPoint

    0 讨论(0)
  • 2020-12-21 03:46

    This might work. I haven't tested it. It's a modified version of some code I found here.

    function elementWithinElement(elemPossiblyCovered, elemPossiblyCovering)
    {
        var top = elemPossiblyCovered.offsetTop;
        var left = elemPossiblyCovered.offsetLeft;
        var width = elemPossiblyCovered.offsetWidth;
        var height = elemPossiblyCovered.offsetHeight;
    
        while (elemPossiblyCovered.offsetParent)
        {
            elemPossiblyCovered = elemPossiblyCovered.offsetParent;
            top += elemPossiblyCovered.offsetTop;
            left += elemPossiblyCovered.offsetLeft;
        }
    
        return (
        top >= elemPossiblyCovering.offsetTop &&
        left >= elemPossiblyCovering.offsetLeft &&
        (top + height) <= (elemPossiblyCovering.offsetTop + elemPossiblyCovering.offsetHeight) &&
        (left + width) <= (elemPossiblyCovering.offsetLeft + elemPossiblyCovering.offsetWidth)
      );
    }
    

    So it'd be something like:

    if(elementWithinElement(myTextbox, theDatepickerDiv))
    { 
        // It's hidden
    }else
    {
        //It's visible
    }
    

    Edit: Some of the code wasn't updated. Should be fixed now.

    Edit Again: Fixed the code and tested it. It works!

    0 讨论(0)
  • 2020-12-21 03:48

    the only way I can think of is by getting the offset of each item and checking that onclick of something that the offset of the new item isn't within the offset of anything previous. Obviously that just the theory behind it making something that does that will take a long time. Good luck :)

    0 讨论(0)
提交回复
热议问题