daily pastebin goal
14%
SHARE
TWEET

Function check weather element is in viewport

mikele027 Jan 12th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <script>
  3. $.fn.isOnScreen = function() {
  4.     var win = $(window);
  5.     var viewport = {
  6.         top: win.scrollTop(),
  7.         left: win.scrollLeft()
  8.     };
  9.     viewport.right = viewport.left + win.width();
  10.     viewport.bottom = viewport.top + win.height();
  11.  
  12.     var bounds = this.offset();
  13.     bounds.right = bounds.left + this.outerWidth();
  14.     bounds.bottom = bounds.top + this.outerHeight();
  15.  
  16.     return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
  17.  
  18. };
  19. </script>
  20. <script>
  21. if ($('.element').isOnScreen()) {
  22. // do something
  23. }
  24. </script>
RAW Paste Data
Top