Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript函数未给出图像的预期结果_Javascript_Html_Css_Image_Function - Fatal编程技术网

Javascript函数未给出图像的预期结果

Javascript函数未给出图像的预期结果,javascript,html,css,image,function,Javascript,Html,Css,Image,Function,运行此代码时,应调整图像的高度和宽度以适合容器 这是代码(警报)的输出: 2488:图像自然高度 3264:图像自然宽度 450:容器高度 1063:容器宽度 612:新高度 844:新宽度 4:它被分割以获得输出的次数 **应将其划分6次,以提供以下结果: 新宽度:544 新高度:414 ** 我几乎可以肯定问题出在Java脚本中: 函数调整大小(iid、eid){ //获取元素的ID(ele是图像所在的容器,img是图像本身) var img=document.getElementById

运行此代码时,应调整图像的高度和宽度以适合容器

这是代码(警报)的输出:

2488:图像自然高度

3264:图像自然宽度

450:容器高度

1063:容器宽度

612:新高度

844:新宽度

4:它被分割以获得输出的次数

**应将其划分6次,以提供以下结果:

新宽度:544

新高度:414 **

我几乎可以肯定问题出在Java脚本中:

函数调整大小(iid、eid){
//获取元素的ID(ele是图像所在的容器,img是图像本身)
var img=document.getElementById('img');
var ele=document.getElementById('contaner');
//使所需的var
var currentwidth=ele.clientWidth;
var currentheight=电气clientHeight;
var自然高度=img.自然高度;
var naturalwidth=img.naturalwidth;
var newheight=自然高度;
var newwidth=自然宽度;
var x=0;
//运行循环以调整图像大小
while(newheight>currentheight&&newwidth>currentwidth){
x=x+1;
新高度=自然高度/x;
新宽度=自然宽度/x;
}
newheight=Math.ceil(newheight);
newwidth=Math.ceil(newwidth);
//提示答案
警惕(自然高度);
警报(自然宽度);
警报(当前高度);
警报(当前宽度);
警报(新高度);
警报(新宽度);
警报(x);
}
#contaner{
高度:450px;
宽度:90%;
利润率:5%自动;
位置:相对位置;
}
#img{
高度:450px;
宽度:90%;
}

问题在于这一行:

while (newheight > currentheight && newwidth > currentwidth)
当宽度或高度适合容器时,它就会停止,因为看起来您希望两者都适合容器的边界。更改为
|
,您将获得六次迭代:

while (newheight > currentheight || newwidth > currentwidth)