Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery jslint var在定义之前就已经被使用了_Javascript_Jquery_Jslint - Fatal编程技术网

Javascript jQuery jslint var在定义之前就已经被使用了

Javascript jQuery jslint var在定义之前就已经被使用了,javascript,jquery,jslint,Javascript,Jquery,Jslint,我一直在使用jslint来尝试了解它对我的代码的影响,我得到了很多标志,但我正在努力改进它。然而,我被错误缠住了 定义maxHeight之前已使用它 我的jQuery: $.fn.thumbSizr = function () { // begin function "use strict"; return this.each(function () { var $this = $(this); maxWidth = $(this).par

我一直在使用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 );
   });
};
$.fn.thumbsizer=function(){//begin function
“严格使用”;
返回此。每个(函数(){
var$this=$(this);
maxWidth=$(this.parent().width();//图像的最大宽度
minHeight=$(this.parent().height();//图像的最大高度
ratio=0;//用于纵横比
宽度=$(this).width();//当前图像宽度
高度=$(this).height();//当前图像高度
如果(宽度>最大宽度){
ratio=maxWidth/width;//获取缩放图像的比率
$(this.css(“width”,maxWidth);//设置新宽度
$(this).css(“高度”,高度*比率);//根据比率缩放高度
高度=高度*比率;//重置高度以匹配缩放图像
宽度=宽度*比率;//重置宽度以匹配缩放图像
}
//检查当前高度是否大于最大值
如果(高度<最小高度){
ratio=minHeight/height;//获取缩放图像的比率
$(this.css(“height”,minHeight);//设置新高度
$(this).css(“宽度”,宽度*比率);//基于比率缩放宽度
宽度=宽度*比率;//重置宽度以匹配缩放图像
}
var$img=$(此),
css={
位置:'绝对',
marginLeft:“-”+(parseInt($img.css('width'))/2)+“px”,
左:50%,
前50%,
marginTop:“-”+(parseInt($img.css('height'))/2)+“px”
};
$img.css(css);
});
};
我不是jQuery专业人士,所以这可能有点不恰当,但我真的想让它尽可能好。有人能解释并建议我为什么会收到这个信息,以及将来如何避免它吗


谢谢

当用单个“var”声明多个变量时,您使用的是分号而不是逗号

这部分是错误的:

 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
固定的:

 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

妈的,我真不好。谢谢你,伙计。当它让我在你的代码中找不到
maxHeight
时,我会接受答案。好的,我想它不再使用了。我会移除它。