Finding if element is visible (JavaScript )

前端 未结 4 1514
悲哀的现实
悲哀的现实 2021-02-05 19:44

I have a javascript function that tries to determine whether a div is visible and does various processes with that variable. I am successfully able to swap an elements visibilit

4条回答
  •  暖寄归人
    2021-02-05 20:07

    Let's take a second to see what .is(":visible") is doing in jQuery, shall we?

    Here's a link: https://github.com/jquery/jquery/blob/master/src/css.js#L529

    return !jQuery.expr.filters.hidden( elem );

    where

    jQuery.expr.filters.hidden = function( elem ) {
        // Support: Opera <= 12.12
        // Opera reports offsetWidths and offsetHeights less than zero on some elements
        return elem.offsetWidth <= 0 && elem.offsetHeight <= 0;
    };
    

    So, it's just checking the offset width and height of the element.

    That said, and also worth noting, when jQuery checks to see if an element is hidden (i.e. like when triggering a 'toggle' event), it performs a check on the display property and its existence in the dom. https://github.com/jquery/jquery/blob/master/src/css.js#L43

提交回复
热议问题