Check if element is visible in DOM

后端 未结 18 1956

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:02

    This may help : Hide the element by positioning it on far most left position and then check the offsetLeft property. If you want to use jQuery you can simply check the :visible selector and get the visibility state of the element.

    HTML :

    Hello

    CSS :

    
    #myDiv{
       position:absolute;
       left : -2000px;
    }
    
    
    #myDiv{
        visibility:hidden;
    }
    

    javaScript :

    var myStyle = document.getElementById("myDiv").offsetLeft;
    
    if(myStyle < 0){
         alert("Div is hidden!!");
    }
    

    jQuery :

    if(  $("#MyElement").is(":visible") == true )
    {  
         alert("Div is visible!!");        
    }
    

    jsFiddle

提交回复
热议问题