Checking visibility of an element

This question already has an answer here:

  • How do I check if an element is hidden in jQuery? 53 answers

  • // jQuery no conflict mode
    var j = $.noConflict();
    
    // retain meaning of jQuery's handle (optional but makes it
    // sometimes easier if you don't use one-letter assignments
    // of jQuery)
    (function($){
    
      // document read
      $(function(){
        // if element is visible (a visible #element was found)
        if $('#element:visible').size() > 0){
          // scroll to #target
          $('body').scrollTo('#target');
        }
      });
    
    })(j);
    

    :visible makes it easier. You're can't just test against display=='block' , you'd also have to test for inline-block and others in addition to checking the visibility setting. For example, the element could have display:block:visibility:hidden which doesn't make it :visible .


    Try:

    if($(element).is(":visible"))
    

    Refer to this post: How do I check if an element is hidden in jQuery?


    使用.is()和:可见

    var j = jQuery.noConflict();
    
    jQuery(function($) {
        if($('#element').is(':visible')){
            $('body').scrollTo('#target');
        };
    });
    
    链接地址: http://www.djcxy.com/p/2392.html

    上一篇: 检查div是否可见

    下一篇: 检查元素的可见性