Advertisement
temp10min

lazyload1

Jun 22nd, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name       lazyload
  3. // @description  LAZY Loading Images
  4. // @match      http://*/*
  5. // @match      https://*/*
  6. // @grant       none
  7. // @run-at          document-end
  8. // ==/UserScript==
  9.  
  10. //
  11. //  LAZY Loading Images
  12. //
  13. //  Handles lazy loading of images in one or more targeted divs,
  14. //  or in the entire page. It also keeps track of scrolling and
  15. //  resizing events, and removes itself if the work is done.
  16. //
  17. //  Licensed under the terms of the MIT license.
  18. //
  19. //  (c) 2010 Balazs Galambosi
  20. //
  21.  
  22. (function(){
  23.  
  24. // glocal variables
  25. var window    = this,
  26.     instances = {},
  27.     winH;
  28.  
  29. // cross browser event handling
  30. function addEvent( el, type, fn ) {
  31.   if ( window.addEventListener ) {
  32.     el.addEventListener( type, fn, false );
  33.   } else if ( window.attachEvent ) {
  34.     el.attachEvent( "on" + type, fn );
  35.   } else {
  36.     var old = el["on" + type];
  37.     el["on" + type] = function() { old(); fn(); };
  38.   }
  39. }
  40.  
  41. // cross browser event handling
  42. function removeEvent( el, type, fn ) {
  43.   if ( window.removeEventListener ) {
  44.     el.removeEventListener( type, fn, false );
  45.   } else if ( window.attachEvent ) {
  46.     el.detachEvent( "on" + type, fn );
  47.   }
  48. }
  49.  
  50. // cross browser window height
  51. function getWindowHeight() {
  52.   if ( window.innerHeight ) {
  53.     winH = window.innerHeight;
  54.   } else if ( document.documentElement.clientHeight ) {
  55.     winH = document.documentElement.clientHeight;
  56.   } else if ( document.body && document.body.clientHeight ) {
  57.     winH = document.body.clientHeight;
  58.   } else {        // fallback:
  59.     winH = 10000; // just load all the images
  60.   }
  61.   return winH;
  62. }
  63.  
  64. // getBoundingClientRect alternative
  65. function findPos(obj) {
  66.   var top  = 0;
  67.   if (obj && obj.offsetParent) {
  68.     do {
  69.       top += obj.offsetTop || 0;
  70.       top -= obj.scrollTop || 0;
  71.     } while (obj = obj.offsetParent); //
  72.     return { "top" : top };
  73.   }
  74. }
  75.  
  76. // top position of an element
  77. var getTopPos = (function() {
  78.   var dummy = document.createElement("div");
  79.   if ( dummy.getBoundingClientRect ) {
  80.     return function( el ) {
  81.       return el.$$top || el.getBoundingClientRect().top;
  82.     };
  83.   } else {
  84.     return function( el ) {
  85.       return el.$$top || findPos( el ).top;
  86.     };
  87.   }
  88. })();
  89.  
  90. // sorts images by their vertical positions
  91. function img_sort( a, b ) {
  92.   return getTopPos( a ) - getTopPos( b );
  93. }
  94.  
  95. // let's just provide some interface
  96. // for the outside world
  97. var LazyImg = function( target, offset ) {
  98.  
  99.   var imgs,    // images array (ordered)
  100.       last,    // last visible image (index)
  101.       id,      // id of the target element
  102.       self;    // this instance
  103.  
  104.   offset = offset || 200; // for prefetching
  105.  
  106.   if ( !target ) {
  107.     target = document;
  108.     id = "$document";
  109.   } else if ( typeof target === "string" ) {
  110.     id = target;
  111.     target = document.getElementById( target );
  112.   } else {
  113.     id = target.id || "$undefined";
  114.   }
  115.  
  116.   // return if this instance already exists
  117.   if ( instances[id] ) {
  118.     return instances[id];
  119.   }
  120.  
  121.   // or make a new instance
  122.   self = instances[id] = {
  123.  
  124.     // init & reset
  125.     init: function() {
  126.       imgs = null;
  127.       last = 0;
  128.       addEvent( window, "scroll", self.fetchImages );
  129.       self.fetchImages();
  130.       return this;
  131.     },
  132.  
  133.     destroy: function() {
  134.       removeEvent( window, "scroll", self.fetchImages );
  135.       delete instances[id];
  136.     },
  137.  
  138.     // fetches images, starting at last (index)
  139.     fetchImages: function() {
  140.  
  141.       var img, temp, len, i;
  142.  
  143.       // still trying to get the target
  144.       target = target || document.getElementById( id );
  145.  
  146.       // if it's the first time
  147.       // initialize images array
  148.       if ( !imgs && target ) {
  149.  
  150.         temp = target.getElementsByTagName( "img" );
  151.  
  152.         if ( temp.length ) {
  153.           imgs = [];
  154.           len  = temp.length;
  155.         } else return;
  156.  
  157.         // fill the array for sorting
  158.         for ( i = 0; i < len; i++ ) {
  159.           img = temp[i];
  160. //          img = temp;
  161.               img.thumb = img.getAttribute("data-src") || img.getAttribute("data-image"); // add here
  162.           if ( img.nodeType === 1 && img.getAttribute("thumb") ) {
  163.  
  164.               // store them and cache current
  165.               // positions for faster sorting
  166.               img.$$top = getTopPos( img );
  167.               imgs.push( img );
  168.           }
  169.         }
  170.         imgs.sort( img_sort );
  171.       }
  172.  
  173.       // loop through the images
  174.       while ( imgs[last] ) {
  175.  
  176.         img = imgs[last];
  177.  
  178.         // delete cached position
  179.         if ( img.$$top ) img.$$top = null;
  180.  
  181.         // check if the img is above the fold
  182.         if ( getTopPos( img ) < winH + offset )  {
  183.  
  184.           // then change the src
  185.           img.src = img.getAttribute("thumb");
  186.           last++;
  187.         }
  188.         else return;
  189.       }
  190.  
  191.       // we've fetched the last image -> finished
  192.       if ( last && last === imgs.length )  {
  193.         self.destroy();
  194.       }
  195.     }  
  196.   };
  197.   return self.init();
  198. };
  199.  
  200. // initialize
  201. getWindowHeight();
  202. addEvent( window, "load",   LazyImg().fetchImages );
  203. addEvent( window, "resize", getWindowHeight       );
  204. LazyImg();
  205.  
  206. window.LazyImg = LazyImg;
  207.  
  208. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement