Skip to content

Commit

Permalink
added aspect ratio plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
aFarkas committed Mar 11, 2015
1 parent e441aa1 commit a08143f
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ For non crucial or below the fold images or in case you want to save more initia
alt="my image" />
```

Note: In case you are using the simple markup pattern, consider to set the ``preloadAfterLoad`` to ``true`` (for SEO) and to add unobtrusive unveil effects ([demo](http://afarkas.github.io/lazysizes/no-src.html#examples)).
Note: In case you are using the simple markup pattern, consider to add unobtrusive unveil effects ([demo](http://afarkas.github.io/lazysizes/no-src.html#examples)).

###The noscript pattern

Expand Down
2 changes: 1 addition & 1 deletion animate.html
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ <h3>responsive image with srcset and <strong>automatic sizes</strong> attribute<

<div class="row">
<div class="alert alert-danger">
<p>Note that you must use either <a href="https://github.com/scottjehl/picturefill" target="_blank">picturefill</a> or <a href="https://github.com/aFarkas/respimage">respimage</a> for full <strong>cross browser support of responsive images</strong>.</p>
<p>For responsive images support you must use either use a full polyfill like <a href="https://github.com/scottjehl/picturefill" target="_blank">picturefill</a> or <a href="https://github.com/aFarkas/respimage">respimage</a> or use the <a href="https://github.com/aFarkas/lazysizes/blob/gh-pages/plugins/respimg">extreme lightweight partial respimg polyfill plugin</a> or use the <a href="https://github.com/aFarkas/lazysizes/tree/gh-pages/plugins/rias">Responsive Images as a Service extension</a>.</p>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ <h3>responsive image with srcset and <strong>automatic sizes</strong> attribute<

<div class="row">
<div class="alert alert-danger">
<p>For responsive images support you must use either use a polyfill like <a href="https://github.com/scottjehl/picturefill" target="_blank">picturefill</a> or <a href="https://github.com/aFarkas/respimage">respimage</a> or use the <a href="https://github.com/aFarkas/lazysizes/tree/gh-pages/plugins/rias">Responsive Images as a Service extension</a>.</p>
<p>For responsive images support you must use either use a full polyfill like <a href="https://github.com/scottjehl/picturefill" target="_blank">picturefill</a> or <a href="https://github.com/aFarkas/respimage">respimage</a> or use the <a href="https://github.com/aFarkas/lazysizes/blob/gh-pages/plugins/respimg">extreme lightweight partial respimg polyfill plugin</a> or use the <a href="https://github.com/aFarkas/lazysizes/tree/gh-pages/plugins/rias">Responsive Images as a Service extension</a>.</p>
</div>
</div>

Expand Down
5 changes: 2 additions & 3 deletions lazysizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@

if(isLoading > 6 && (!elemExpandVal || ('src' in lazyloadElems[i]))){continue;}

if(elemExpand > defaultExpand && (loadMode < 2 || isLoading > 3)){
if(elemExpand > shrinkExpand && (loadMode < 2 || isLoading > 3)){
elemExpand = shrinkExpand;
}

Expand All @@ -228,7 +228,6 @@
unveilElement(lazyloadElems[i]);
loadedSomething = true;
} else {

if(Date.now() - start > 3){
checkElementsIndex++;
throttledCheckElements();
Expand Down Expand Up @@ -532,7 +531,7 @@
minSize: 50,
customMedia: {},
init: true,
expFactor: 2,//is the factor that is used to multiply the `expand` option to calc the preload expand
expFactor: 2,
expand: 300
};

Expand Down
4 changes: 2 additions & 2 deletions lazysizes.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion no-src.html
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ <h3>responsive image with srcset and <strong>automatic sizes</strong> attribute<

<div class="row">
<div class="alert alert-danger">
<p>Note that you must use either <a href="https://github.com/scottjehl/picturefill" target="_blank">picturefill</a> or <a href="https://github.com/aFarkas/respimage">respimage</a> for full <strong>cross browser support of responsive images</strong>.</p>
<p>For responsive images support you must use either use a full polyfill like <a href="https://github.com/scottjehl/picturefill" target="_blank">picturefill</a> or <a href="https://github.com/aFarkas/respimage">respimage</a> or use the <a href="https://github.com/aFarkas/lazysizes/blob/gh-pages/plugins/respimg">extreme lightweight partial respimg polyfill plugin</a> or use the <a href="https://github.com/aFarkas/lazysizes/tree/gh-pages/plugins/rias">Responsive Images as a Service extension</a>.</p>
</div>
</div>

Expand Down
172 changes: 172 additions & 0 deletions plugins/aspectratio/ls.aspectratio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
(function(window, document){
'use strict';

var imageRatio, extend$;

var regPicture = /^picture$/i;
var aspectRatioAttr = 'data-aspectratio';
var aspectRatioSel = 'img[' + aspectRatioAttr + ']';

var matchesMedia = function(media){
if(window.matchMedia){
matchesMedia = function(media){
return !media || (matchMedia(media) || {}).matches;
};
} else if(window.Modernizr && Modernizr.mq){
return !media || Modernizr.mq(media);
} else {
return !media;
}
return matchesMedia(media);
};

function AspectRatio(){
this.ratioElems = document.getElementsByClassName('lazyaspectratio');
this._setupEvents();
this.processImages();
}

AspectRatio.prototype = {
_setupEvents: function(){
var module = this;

var addRemoveAspectRatio = function(elem){
if(elem.naturalWidth < 50){
module.addAspectRatio(elem, true);
} else {
module.removeAspectRatio(elem, true);
}
};

document.addEventListener('load', function(e){
if(e.target.getAttribute && e.target.getAttribute(aspectRatioAttr)){
addRemoveAspectRatio(e.target);
}
}, true);

addEventListener('resize', (function(){
var timer;
var resize = function(){
var i, len;
for(i = 0, len = module.ratioElems.length; i < len; i++){
addRemoveAspectRatio(module.ratioElems[i]);
}
};

return function(e){
clearTimeout(timer);
timer = setTimeout(resize, 33);
};
})());

document.addEventListener('DOMContentLoaded', function(){
module.processImages();
});
},
processImages: function(context){
var elements, i;

if(!context){
context = document;
}

if('length' in context && !context.nodeName){
elements = context;
} else {
elements = context.querySelectorAll(aspectRatioSel);
}

for(i = 0; i < elements.length; i++){
if(elements[i].naturalWidth > 50){
this.removeAspectRatio(elements[i]);
continue;
}
this.addAspectRatio(elements[i]);
}
},
getSelectedRatio: function(img){
var i, len, sources, customMedia, ratio;
var parent = img.parentNode;
if(parent && regPicture.test(parent.nodeName || '')){
sources = parent.getElementsByTagName('source');

for(i = 0, len = sources.length; i < len; i++){
customMedia = sources[i].getAttribute('data-media') || sources[i].getAttribute('media');

if(lazySizesConfig.customMedia[customMedia]){
customMedia = lazySizesConfig.customMedia[customMedia];
}

if(matchesMedia(customMedia)){
ratio = sources[i].getAttribute(aspectRatioAttr);
break;
}
}
}

return ratio || img.getAttribute(aspectRatioAttr) || '';
},
parseRatio: (function(){
var regRatio = /^\s*([+\d\.]+)(\s*[\/x]\s*([+\d\.]+))?\s*$/;
var ratioCache = {};
return function(ratio){

if(!ratioCache[ratio] && ratio.match(regRatio)){
if(RegExp.$3){
ratioCache[ratio] = RegExp.$1 / RegExp.$3;
} else {
ratioCache[ratio] = RegExp.$1 * 1;
}
}

return ratioCache[ratio];
};
})(),
addAspectRatio: function(img, notNew){
var ratio;
var width = img.offsetWidth;

if(!notNew){
lazySizes.aC(img, 'lazyaspectratio');
}

if(!width){return;}

ratio = this.getSelectedRatio(img);
ratio = this.parseRatio(ratio);

if(ratio){
img.style.height = (width / ratio) + 'px';
}
},
removeAspectRatio: function(img){
lazySizes.rC(img, 'lazyaspectratio');
img.style.height = '';
img.removeAttribute(aspectRatioAttr);
}
};

extend$ = function(){
var $ = window.jQuery || window.Zepto || window.shoestring;
if($ && $.fn && !$.fn.imageRatio){
$.fn.imageRatio = function(){
imageRatio.processImages(this.find(aspectRatioSel).add(this.filter(aspectRatioSel)));
return this;
};
}
};

extend$();
setTimeout(extend$);

imageRatio = new AspectRatio();

window.imageRatio = imageRatio;

if(typeof module == 'object' && module.exports){
module.exports = imageRatio;
} else if (typeof define == 'function' && define.amd) {
define(imageRatio);
}

})(window, document);
Loading

0 comments on commit a08143f

Please sign in to comment.