If you have variable width images, you can go something like this:
var imgResize = function(contId) {
var imgs = [], totalWidth = 0, ratio = 0, containerWidth = 0;
var cont = document.getElementById(contId);
var child = cont.firstChild;
while(child.nextSibling){
if(child.tagName && child.tagName.toUpperCase() === 'IMG'){
var imgW = child.clientWidth;
imgs.push({node: child, width: imgW});
totalWidth += imgW;
}
child = child.nextSibling;
}
ratio = cont.clientWidth / totalWidth;
imgs.forEach(function(img){
var cWidth = Math.floor(ratio*img.width);
img.node.style.cssText='width:'+cWidth+'px';
});
};
window.onresize=function(){imgResize('container');};
Keep in mind that you should probably do something like
img{border:0px;display:block;float:right;}
.imgContainer{font-size:0px;padding:0px;width:100%;}
Getting this to work in less modern browsers remains as an exercise for the reader. And yes, using the framework is a great idea, I would recommend YUI, so good that even the Russians, as you know, used it.
source
share