2015-10-18 65 views
0

我正在從我的一個函數中獲得NaN。在做了研究之後,我發現答案不是數字,但我認爲這一定是可以接受的。我只用數字工作,我正在回答這個問題。圖片調整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> 

回答

0

您有:

var x; 

然後當你這樣做:

x = x + 1; 

xundefined它獲得CAS噸到字符串,然後用1。所以級聯:

x = 'undefined1'; 

x被用在它返回NaN一個部門。

解決方案:更改x聲明:

var x = 0; 
0

的NaN是由這行代碼由於:

var x; 

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