gpt4 book ai didi

javascript - 图像调整大小 NaN 错误

转载 作者:行者123 更新时间:2023-11-28 07:01:34 24 4
gpt4 key购买 nike

我正在从我的一个函数中取回 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;

xundefined 它被转换为字符串,然后与 1 连接。所以:

x = 'undefined1';

x 用于除法时,它返回 NaN。

解决方案:更改x声明:

var x = 0;

关于javascript - 图像调整大小 NaN 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com