Javascript - 调整幻灯片图像的大小

时间:2014-06-08 12:27:59

标签: javascript image image-resizing

我正在尝试使用JavaScript制作幻灯片,我想只显示一半图像,但我遇到了问题。在Chrome和Opera中,某些图像的宽度和高度等于0。为什么呢?

HTML

<img id="picArt" src="0.jpg" onload="resizeImage();" /> <br />
<input type="button" value="Back" onclick="slidePic('P');" />
<input type="button" value="Forward" onclick="slidePic('N');" />

的JavaScript

var picArt = document.getElementById("picArt");
var picLocal = new Array();

for(var num=0;num<=17;num++) {
   picLocal[num] = String(num) +".jpg";
} 

var desrib = new Array();

var preLoad = new Array();
var next = 0, picLength = (picLocal.length-1);
for(var num=0;num <= picLength; num++) {
    preLoad[num] = new Image();
    preLoad[num].src = picLocal[num];
}

function slidePic (how) {
    if(how == "N") next++;
    if(how == "P") next--;
    if(next> picLength) next =0;
    if(next <0) next = picLength;

    picArt.src = preLoad[next].src; 
}

function resizeImage() {
    var originImage = new Image();
    originImage.src = picArt.src;

    picArt.width = originImage.width/2;
    picArt.height = originImage.height/2;
}

1 个答案:

答案 0 :(得分:0)

可能会在CSS中调整图像大小,而不是JavaScript吗? 例如:

img{
  width:200px;
  max-width:200px;
}
  • 自动调整宽度和高度,不使用js