Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement