Javascript: How to check if element is visible?

青春壹個敷衍的年華 提交于 2019-12-09 07:31:25
sdespont

You can check the display CSS property:

 function show_guides() {

        $('#guides').toggle();

        if ( $('#guides').css('display') == 'block' ) { 
            console.log("visible");
        } else {
            console.log("invisible");
        }
    }

try

     style.display="block";

and

     style.display="hidden";

You can check visibility:visible/hidden, or display:block/none

$('#guides').css('visibility') == 'visible'
$('#guides').css('display') == 'block'

If all you want is to check visibility you can just use this

  function visible(elem){
    elem = $(elem)
    return !!(elem.width() || elem.height()) && elem.css("display") !== "none"
  }

taken straight from the zepto selectors plugin. Otherwise you can just include the selectors module from https://github.com/madrobby/zepto/blob/master/src/selector.js as Felix Kling suggested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!