我昨晚创建了这个图像大小调整插件。问题是如果只有一个“div.content”,我可以执行以下操作来使其工作:
window.onload=function(){
$('div.content').image_resizer();
}
但是当我添加更多这样的“div.content”并执行以下操作时:
window.onload=function(){
$.each($('div.content'),function(k,v){
$(this).image_resizer();
});
}
该插件适用于第一个 div.content 并在“div.content”的其余部分失败。你能告诉我我做错了什么吗?
[编辑]这里是标记:
<div class="content">
<img src="./images/img5.jpg" id='p1'/>
</div>
[编辑 2] 还有一件事,当我
$.each($('div.content'),function(k,v){$(this).image_resizer()});
手动输入 chrome 的控制台时。所有图像都会调整大小以适应它们各自的 div。
[插件代码]
(function($){
$.fn.image_resizer = function(options){
$.fn.image_resizer.defaults = { MAX_HEIGHT : 280,MAX_WIDTH : 330,BORDER_WIDTH : 10 }
var opts = $.extend({},$.fn.image_resizer.defaults,options);
return this.each(function(){
//plugin code starts here
// getting array of all the images inside 'di$(this)' on which this plugin is applied
var arr = $(this).find('img');
//iterating o$(this)er the array 'arr'
$.each(arr,function(k){
//console.log(k,$(this))
// now resizing the image
img_hit=$(this).height();
img_wit=$(this).width();
//console.log(img_hit,img_wit);
//calculating image height to width ratio
var ratio = img_hit/img_wit;
if(img_wit>img_hit){
//console.log("wit module");
if(img_wit>opts.MAX_WIDTH){
//console.log("Image needs resizing");
new_wit=opts.MAX_WIDTH-(opts.BORDER_WIDTH*2);
$(this).width(new_wit);
//console.log(new_wit);
var new_hit = ratio*new_wit;
$(this).height(new_hit)
var space = opts.MAX_HEIGHT-(new_hit+(opts.BORDER_WIDTH*2));
//console.log(space);
$(this).css('margin-top',space/2);
$(this).css('margin-bottom',space/2);
}else{
//console.log("Image doesn't need resizing")
var space = opts.MAX_HEIGHT-(img_hit+(opts.BORDER_WIDTH*2));
//console.log(space);
$(this).css('margin-top',space/2);
$(this).css('margin-bottom',space/2);
}
}else{
//console.log("hit module");
if(img_hit>opts.MAX_HEIGHT){
//console.log("Image needs resizing");
new_hit = opts.MAX_HEIGHT-(opts.BORDER_WIDTH*2);
$(this).height(new_hit);
var new_wit = new_hit/ratio;
$(this).width(new_wit);
}else{
//console.log("Image doesn't need resizing");
var space = opts.MAX_HEIGHT-(img_hit+(opts.BORDER_WIDTH*2));
//console.log(space);
$(this).css('margin-top',space/2);
$(this).css('margin-bottom',space/2);
}
}
});
//plugin code ends here
});
}
})(jQuery);