javascript jquery max-height 和 max-width 返回错误的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9061764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:28:53  来源:igfitidea点击:

jquery max-height and max-width returns wrong values

javascriptjqueryhtmlcss

提问by Israhack

im having a problem with max-width and max-height. lets say i have the following css rule:

我有最大宽度和最大高度的问题。假设我有以下 css 规则:

img.item { max-width: 325px; max-height: 390px; }

and the following img in my document:

以及我的文档中的以下 img:

<img class="item" src="http://us.dorothyperkins.com/wcsstore/DorothyPerkinsUS/images/catalog/80000095_normal.jpg">

original image width x height is: 342 x 464

原始图像宽 x 高为:342 x 464

if i try and get with jquery the values for max-width and max-height i get them wrong.

如果我尝试使用 jquery 获取 max-width 和 max-height 的值,我会弄错。

$('img.item').css('max-width');
// returns 357px
$('img.item').css('max-height');
// returns 429px

any ideas on why is this? or how i can get the correct sizes?

关于为什么会这样的任何想法?或者我怎样才能得到正确的尺寸?

if i use $('img.item').width() and $('img.item').height()i get the calculated values, not good for me.

如果我使用$('img.item').width() and $('img.item').height()我得到计算值,对我不利。

thanks.

谢谢。

回答by Juris Vaiders

Strange results. I have created fiddle on http://jsfiddle.net/JGwqY/and there max-width and max-height values are correct.

奇怪的结果。我在http://jsfiddle.net/JGwqY/上创建了小提琴,并且 max-width 和 max-height 值是正确的。

回答by Bhavin Solanki

I have check you code and add latest jquery, it works well with the correct result. Please check my code here.

我已经检查了你的代码并添加了最新的 jquery,它可以很好地处理正确的结果。请在此处检查我的代码。

alert($('img.item').css('max-width'));

alert($('img.item').css('max-height'));
img.item { max-width: 325px; max-height: 390px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="item" src="http://us.dorothyperkins.com/wcsstore/DorothyPerkinsUS/images/catalog/80000095_normal.jpg">

There might me other reason is, Your image max-heightand max-widthmight be overwriteby other class

有可能我的另外一个原因是,你的形象max-heightmax-width可能覆盖其他class