Categories: Javascript Tricks

How to check visibility change of any elements in jQuery

Hi guys, I hope you are doing good. Today we are going to discuss about how to check visibility of any element using JavaScript and jQuery.

Since I am a web developer, I have faced many scenario where we have to perform operations on element visibility. So I am going to show you some simple and useful tricks to check elements visibility.

JavaScript, jQuery & Ajax

Visibility checking of any elements.

Option 1 : Using JavaScript

<script>
  function hiddenFunction()
  {
    console.log('Now element become hidden');
  }
  function visibleFunction()
  {
    console.log('Now element become visible');
  }
  if(document.getElementById('selectedTag').css('display')=='none')
  {
  hiddenFunction();
  }else
  {
  	visibleFunction();
  }
</script>

Option 2 : Using jQuery

<script>
  function hiddenFunction()
  {
    console.log('Now element become hidden');
  }
  function visibleFunction()
  {
    console.log('Now element become visible');
  }
  if(jQuery('selectedTag').is(":visible"))
  {
  	visibleFunction();
  }else
  {
  	hiddenFunction();
  }
</script>

Option 3 : Visibility checking using jQuery on dynamic visibility changing

This is little bit tricky because whenever you have check elements visibility on window load or DOM Ready , you can use either of both above options. But the element is getting visible on some events either by user interaction or by some periodic/loop condition, it is really hard to check. And there is no predefined function available till date as date, by which you can check it by using any external script. So here it is, what I have come across.

<script>
  function hiddenFunction()
  {
    console.log('Now element become hidden');
  }
  function visibleFunction()
  {
    console.log('Now element become visible');
  }
  var visibilityCounter=0;
  $( '#selectedTag' ).on( 'visibility', function() {
    var $element = $( this );
    var timer = setInterval( function() {
      if( $element.is( ':hidden' ) ) {
        hiddenFunction();
        visibilityCounter=0;
      } else {
        if(visibilityCounter==0)
        {
          visibilityCounter=1;
          visibleFunction();
        }
      }
    }, 300 );
  }).trigger( 'visibility' );
</script>

Here I am using setInterval() function to check visibility to check elements Visiblity. You can use it as external script to check element’s visibility.
If you have access to change exiting core code, I would suggest edit that to avoid extra loop load.
By changing exiting code you can increase your website performance.

I hope this blog will be useful for you. If you need anything else about this blog or apart from this I would love to hear that, Just you have to leave comment or you can use get in touch option to contact us

Notes On Click

Share
Published by
Notes On Click
Tags: ajaxjavascript tricksjQueryscripts

Recent Posts

  • Adword Conversion Tracking
  • Google Adwords Remarketing

Conversion and Remaketing code in BigCommerce

In this blog, you will see how to add Adwords Conversion Tracking and Remarkeing Code in BigCommerce.BigCommerce is one of…

8 years ago
  • Google Analytics and Ecommerce Tracking

Ecommerce Tracking in OpenCart 2.3

Around 6 months back, I had published an article on “How to add GA e-commerce tracking in Opencart“. I am really thankful…

8 years ago
  • Google Analytics and Ecommerce Tracking

Google Analytics Definition and Common Terms

 If you have gone through our previous blog about Google Analytics, you might have sound understanding about Google Analytics and how it…

8 years ago
  • Google Analytics and Ecommerce Tracking

What is Google Analytics and it’s Features?

Do you want to know more about your visitors and how your content is performing?​ Whether you run a website…

8 years ago
  • Google Analytics and Ecommerce Tracking

How to Add Google Analytics in BigCommerce

In this blog, you will see how to add Google Analytics and E-commerce Tracking Code  in BigCommerce. BigCommerce is one of the…

8 years ago
  • Google Analytics and Ecommerce Tracking

Google Analytics and E-commerce Tracking in Miva Merchant

If you have gone through some of previous blog, you might have seen how to install google analytics, e-commerce tracking…

8 years ago