【发布时间】:2013-12-18 03:21:06
【问题描述】:
我一直在使用 jslint 来尝试查看它对我的代码的说明,我得到了很多标志,但我正在努力改进它。但是我被错误困住了
在定义之前使用了maxHeight
我的 jQuery:
$.fn.thumbSizr = function () { // begin function
"use strict";
return this.each(function () {
var $this = $(this);
maxWidth = $(this).parent().width(); // Max width for the image
minHeight = $(this).parent().height(); // Max height for the image
ratio = 0; // Used for aspect ratio
width = $(this).width(); // Current image width
height = $(this).height(); // Current image height
if(width > maxWidth){
ratio = maxWidth / width; // get ratio for scaling image
$(this).css("width", maxWidth); // Set new width
$(this).css("height", height * ratio); // Scale height based on ratio
height = height * ratio; // Reset height to match scaled image
width = width * ratio; // Reset width to match scaled image
}
// Check if current height is larger than max
if(height < minHeight){
ratio = minHeight / height; // get ratio for scaling image
$(this).css("height", minHeight); // Set new height
$(this).css("width", width * ratio); // Scale width based on ratio
width = width * ratio; // Reset width to match scaled image
}
var $img = $(this),
css = {
position: 'absolute',
marginLeft: '-' + (parseInt( $img.css('width') ) / 2) + 'px',
left: '50%',
top: '50%',
marginTop: '-' + (parseInt( $img.css('height') ) / 2) + 'px'
};
$img.css( css );
});
};
我不是 jQuery 专业人士,所以这可能有点啰嗦,但我真的想让它尽可能好。谁能解释并建议我为什么收到此消息以及将来如何避免它?
谢谢
【问题讨论】:
-
我似乎在你的代码中找不到
maxHeight -
好点,我认为它不再使用了。我会删除它。
标签: javascript jquery jslint