图像调整NaN错误

时间:2015-10-18 17:47:01

标签: javascript css html5 image

我从我的一个职能部门回来了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>

2 个答案:

答案 0 :(得分:0)

你有:

stop

然后当你这样做:

var x;

x = x + 1; x它被强制转换为字符串,然后与1.连接。所以:

undefined

在分区上使用x = 'undefined1'; 时,它会返回NaN。

解决方案:更改x声明:

x

答案 1 :(得分:0)

NaN 是由以下代码行所致:

var x;

while (newheight > currentheight && newwidth > currentwidth) {
     x = x + 1; //<- Here x is undefined
}