1/* lazyload.js (c) Lorenzo Giuliani 2 * MIT License (http://www.opensource.org/licenses/mit-license.html) 3 * 4 * expects a list of: 5 * `<img src="blank.gif" data-src="my_image.png" width="600" height="400" class="lazy">` 6 */ 7 8!function(window){ 9 var $q = function(q, res){ 10 if (document.querySelectorAll) { 11 res = document.querySelectorAll(q); 12 } else { 13 var d=document 14 , a=d.styleSheets[0] || d.createStyleSheet(); 15 a.addRule(q,'f:b'); 16 for(var l=d.all,b=0,c=[],f=l.length;b<f;b++) 17 l[b].currentStyle.f && c.push(l[b]); 18 19 a.removeRule(0); 20 res = c; 21 } 22 return res; 23 } 24 , addEventListener = function(evt, fn){ 25 window.addEventListener 26 ? this.addEventListener(evt, fn, false) 27 : (window.attachEvent) 28 ? this.attachEvent('on' + evt, fn) 29 : this['on' + evt] = fn; 30 } 31 , _has = function(obj, key) { 32 return Object.prototype.hasOwnProperty.call(obj, key); 33 } 34 ; 35 36 function loadImage (el, fn) { 37 var img = new Image() 38 , src = el.getAttribute('data-src'); 39 img.onload = function() { 40 if (!! el.parent) 41 el.parent.replaceChild(img, el) 42 else 43 el.src = src; 44 45 fn? fn() : null; 46 } 47 img.src = src; 48 } 49 50 function elementInViewport(el) { 51 var rect = el.getBoundingClientRect() 52 53 return ( 54 rect.top >= 0 55 && rect.left >= 0 56 && rect.top <= (window.innerHeight || document.documentElement.clientHeight) 57 ) 58 } 59 60 var images = new Array() 61 , query = $q('img.lazy') 62 , processScroll = function(){ 63 for (var i = 0; i < images.length; i++) { 64 if (elementInViewport(images[i])) { 65 loadImage(images[i], function () { 66 images.splice(i, i); 67 }); 68 } 69 }; 70 } 71 ; 72 // Array.prototype.slice.call is not callable under our lovely IE8 73 for (var i = 0; i < query.length; i++) { 74 images.push(query[i]); 75 }; 76 77 processScroll(); 78 addEventListener('scroll',processScroll); 79 80}(this);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.