elmPosiz - Get position, size and visibility in viewport of HTML element

The elmPosiz is a JS script that gets some useful data of a HTML element in page, like: top and left position, width and height size, and percentage visible in viewport.
This script can be used to check if a Div, image or other html element is in viewport, also, to get its top/left position and size.
It works on tablets, mobile and desktop devices; in all major browsers: Chrome, Mozilla Firefox, Internet Explorer, Opera, Safary.

The JavaScript object created with elmPosiz() has these properties: - These parameters are actualised every time the window is resized or the page scrolls, so you can detect when an element is entering or exits viewport.

Usage

  1. Include the "elmposiz.js" in your page, AFTER the HTML tags that you want to use with this script.
    <script src="elmposiz.js"></script>
  2. Create an object instance with the elmPosiz() function, passing the CSS selector that represents the HTML element, usually an ID.
    <script>
    var ob_nm = new elmPosiz('#the_id');
    </script>
    
  3. Now you can use the "ob_nm" object properties, presented above.
    - Here is a simple example:
    <div id="dv1">Div content</div>
    <script src="elmposiz.js"></script>
    <script>
    //create object with the elmPosiz() for #dv1
    var ob_dv1 = new elmPosiz('#dv1');
    
    //check if #dv1 is minimum 50% visible in viewport
    //shows an alert message with top/left position
    var dv1_vi = ob_dv1.in_view ?'Visible in viewport' :'Not 50% visible in viewport';
    alert('Top: '+ob_dv1.top+' / Left: '+ob_dv1.left+'\n'+dv1_vi);
    </script>
    
    - Another example. We set a function to the "listen" property to check when the Div is not 70% visible in viewport, and shows in console the visible percentage.
    <div id="dv1">Div content</div>
    <script src="elmposiz.js"></script>
    <script>
    //create object with the elmPosiz() for #dv1
    var ob_dv1 = new elmPosiz('#dv1');
    
    //change minimum percentage to 70% for #dv1 to be considered visible in viewport
    ob_dv1.min_visible = 70;
    
    //set a function to the listen property to check #dv1 visibility in viewport,
    //and shows in console the visible percentage
    ob_dv1.listen = function(){
      if(!ob_dv1.in_view) console.log('Elm not 70% in viewport, visible: ', ob_dv1.visibleY);
    }
    </script>
    


• Scipt Page: http://coursesweb.net/javascript/position-size-visibility-viewport-html


- This script is Free, and Open Source. You can modify and use it freely.
- Have a Happy Life with Everyone -