Check if element is visible in DOM

后端 未结 18 1935

Is there any way that I can check if an element is visible in pure JS (no jQuery) ?

So, for example, in this page: Performance Bikes, if you hover over Deals (on the

18条回答
  •  温柔的废话
    2020-11-22 08:15

    All the other solutions broke for some situation for me..

    See the winning answer breaking at:

    http://plnkr.co/edit/6CSCA2fe4Gqt4jCBP2wu?p=preview

    Eventually, I decided that the best solution was $(elem).is(':visible') - however, this is not pure javascript. it is jquery..

    so I peeked at their source and found what I wanted

    jQuery.expr.filters.visible = function( elem ) {
        return !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
    };
    

    This is the source: https://github.com/jquery/jquery/blob/master/src/css/hiddenVisibleSelectors.js

提交回复
热议问题