SHARE
TWEET

Untitled

a guest Mar 18th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. _getPercentInView = function(element){
  2.     $element = $(element);
  3.  
  4.     var pos                     = $element.offset(),
  5.         theViewport             = {top:null, left:null, bottom:null, right:null, width:null, height:null},
  6.         theElement              = {top:null, left:null, bottom:null, right:null, width:null, height:null},
  7.         elemLeft                = pos.left,
  8.         elemTop                 = pos.top,
  9.         elemHeight              = $element.height(),
  10.         elemWidth               = $element.width();
  11.  
  12.     theViewport.width       = $(window).width();
  13.     theViewport.height      = $(window).height();
  14.     theViewport.top         = $(window).scrollTop();
  15.     theViewport.left        = $(window).scrollLeft();
  16.     theViewport.bottom      = theViewport.top+theViewport.height;
  17.     theViewport.right       = theViewport.left+theViewport.width;
  18.  
  19.     theElement.top          = elemTop - theViewport.top;
  20.     theElement.left         = elemLeft - theViewport.left;
  21.     theElement.width        = elemWidth;
  22.     theElement.height       = elemHeight;
  23.     theElement.bottom       = theElement.top+theElement.height;
  24.     theElement.right        = theElement.left+theElement.width;
  25.  
  26.     var nPercentInView = Math.round(100 * ((theElement.height-(Math.max(0,0-theElement.top) + Math.max(0,theElement.bottom-theViewport.height))) / theElement.height) * ((theElement.width-(Math.max(0,0-theElement.left) + Math.max(0,theElement.right-theViewport.width))) / theElement.width)   );
  27.     return nPercentInView;
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top