用css设定图片的width height就能缩放吧?
解决方案 »
- php图片预览问题
- 这个javascript为什么不能得到杀毒软件是否更新呢? 急
- 200分求 WebBrowser.ExecWB指定打印(另存为)的成功代码 100分后补
- js中如何把2009年2月28日 12:23转换成2009-02-28 12:23的形式??
- JavaScript获取HTML源码截取指定变量值并输出
- 我一打开总页面,怎么实现每个页面报警?
- 全选是实现了,那全部取消怎么实现呢?
- js如何读取已经存在的cookies
- 项目中的菜单问题,今天和同事研究了一下午没解决,请大家帮忙给看看,谢谢!
- 请教一下关于document.form1.select1.options[]的问题,请帮忙!!!
- 移动选择(select——option)标签,为什么点击之后不能一次全部移动?
- 如果字符串是一个匿名函数,可以让它运行吗?
我是初学者,请不要见怪。如果纯粹使用JQUERY来控制CSS改变图片尺寸,那样我也懂,使用
$("img").css({ "height": 128px, "width": 128px });
大概这样就行
只是我也看过网上很多JQUERY缩放图片的插件,代码比想象中的要复杂很多,例如下面这个,很大一部分我搞不懂有什么区别:
; (function ($) { $.fn.aeImageResize = function (params) { var aspectRatio = 0; // We cannot do much unless we have one of these
if (!params.height && !params.width) {
return this;
} // Calculate aspect ratio now, if possible
if (params.height && params.width) {
aspectRatio = params.width / params.height;
} // Attach handler to load
// Handler is executed just once per element
// Load event required for Webkit browsers
return this.one("load", function () { // Remove all attributes and CSS rules
this.removeAttribute("height");
this.removeAttribute("width");
this.style.height = this.style.width = ""; var imgHeight = this.height
, imgWidth = this.width
, imgAspectRatio = imgWidth / imgHeight
, bxHeight = params.height
, bxWidth = params.width
, bxAspectRatio = aspectRatio; // Work the magic!
// If one parameter is missing, we just force calculate it
if (!bxAspectRatio) {
if (bxHeight) {
bxAspectRatio = imgAspectRatio + 1;
} else {
bxAspectRatio = imgAspectRatio - 1;
}
} // Only resize the images that need resizing
if ((bxHeight && imgHeight > bxHeight) || (bxWidth && imgWidth > bxWidth)) { if (imgAspectRatio > bxAspectRatio) {
bxHeight = ~~(imgHeight / imgWidth * bxWidth);
} else {
bxWidth = ~~(imgWidth / imgHeight * bxHeight);
} this.height = bxHeight;
this.width = bxWidth;
}
})
.each(function () { // Trigger load event (for Gecko and MSIE)
if (this.complete) {
$(this).trigger("load");
} // This fixes IE9 issue
this.src = this.src;
});
};
})(jQuery);
$(window).resize(function(){
var w = $(document).width();//获取窗口的宽度
$("img").each(function(){ //循环所有图片
var _this = $(this),img = new Image();
img.src = _this.attr("src");
if (img.complete) {
img.width > w ? _this.attr({width:w,height:~~(w*img.height/img.width)}) : _this.attr({width:img.width,height:img.height});
} else {
img.onload = function () {
img.width > w ? _this.attr({width:w,height:~~(w*img.height/img.width)}) : _this.attr({width:img.width,height:img.height});
img.onload = null;
}
}
});
}).resize();