在响应式图像jquery大小调整器上将最大大小设置为本机图像大小

时间:2012-04-21 05:36:10

标签: jquery responsive-design

预警:我是一个重要的新手。

我使用的这些代码根据浏览器窗口的大小调整了我网站上的图像大小。问题是,图像也通过其原始尺寸被放大,从而产生丑陋的图像。像素图像。我想知道是否:

有一种方法可以添加一些代码,以防止图片放大超过原来上传的尺寸。

非常感谢任何帮助。

以下是我目前的情况:

(function( $ ) {

$.fn.aeImageResize = function( params ) {

var aspectRatio = 0
// Nasty I know but it's done only once, so not too bad I guess
// Alternate suggestions welcome :)
,   isIE6 = $.browser.msie && (6 == ~~ $.browser.version)
;

// 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.style.height = "";

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 ( imgAspectRatio > bxAspectRatio ) {
bxHeight = ~~ ( imgHeight / imgWidth * bxWidth );
} else {
bxWidth = ~~ ( imgWidth / imgHeight * bxHeight );
}

this.height = bxHeight-100;

})
.each(function() {

// Trigger load event (for Gecko and MSIE)
if ( this.complete || isIE6 ) {
$( this ).trigger( "load" );
}
});
};
})( jQuery );

1 个答案:

答案 0 :(得分:0)

在函数开头尝试:

if (params.height < this.height ||
    params.width < this.width)
{
  return this;
}