图像调整大小NaN错误
Image Resize NaN Error
我从我的一个函数中得到NaN。经过一番研究,我发现答案不是一个数字,但我认为这肯定是不可能的。我只处理数字,我正在四舍五入的答案。
function resize(iid, eid) {
//Get the ID of the elements (ele being the container that the image is in and img being the image its self)
var img = document.getElementById('img');
var ele = document.getElementById('contaner');
//makes the var needed
var currentwidth = ele.clientWidth;
var currentheight = ele.clientHeight;
var naturalwidth = img.naturalHeight;
var naturalheight = img.naturalWidth;
var newheight = naturalheight;
var newwidth = naturalwidth;
var x;
//runs a loop that should size the image
while (newheight > currentheight && newwidth > currentwidth){
x = x + 1;
newheight = naturalheight / x;
newwidth = naturalwidth / x;
}
newheight = Math.round(newheight);
newwidth = Math.round(newwidth);
//alerts out the answers
alert(newheight);
alert(newwidth)
}
#contaner {
height: 450px;
width: 90%;
margin: 5% auto;
position: relative;
}
#img {
height: 450px;
width: 90%;
}
<div id="contaner">
<img src = "..'..'Resorces'Images'SlideShow'img1.jpg" style="width:652px;height:489px;" id="img"/>
<div id="left_holder"><img onClick="slide(-1)" src="..'..'Resorces'Images'arrow_left.png" class="left"/></div>
<div id="right_holder"><img onClick="slide(+1)" src="..'..'Resorces'Images'arrow_right.png" class="right"/></div>
</div>
你有:
var x;
当你这样做的时候:
x = x + 1;
x
是undefined
,它被强制转换为字符串,然后与1连接。所以:
x = 'undefined1';
当x
用于除法时,它返回NaN。
解决方案:修改x
声明:
var x = 0;
NaN由这行代码完成:
var x;
while (newheight > currentheight && newwidth > currentwidth) {
x = x + 1; //<- Here x is undefined
}
相关文章:
- jquery中出现NaN错误.已经尝试过ParseInt,但仍然不起作用
- 我的jquery中出现NaN错误
- JavaScript NaN错误,输入文本显示为NaN
- NaN错误和数字保持不变
- 如何检查NaN错误的值
- jquery日期分析器错误获取NaN错误
- Javascript-将字符串转换为Int会返回NaN错误
- NaN 错误代码有什么问题
- 为什么此函数返回 Nan 错误
- javascript中的NaN错误,但在调试时结果正确
- 仅正则表达式 Nan 错误号
- 在magento上的倒计时计时器中出现NaN错误.知道为什么吗
- JavaScript Array.map(parseInt)导致NaN错误
- Nan未定义Nan错误(Firefox、Edge和Safari)
- 将变量传递给 getElementById 会导致 NaN 错误
- 结果得到NaN错误
- JS数字到罗马数字脚本中的NaN错误
- 当我单击总和,乘除按钮时,我收到“ NaN”错误
- JavaScript NaN错误循环
- D3.js-如何在拖动折线图时解决NAN错误(Jsfidle提供)