简体   繁体   中英

Javascript - Resizing Slideshow Images

I am trying to make a slideshow in JavaScript, and I want to show only half images, but I have a problem. Some images have width and height equal to 0 in Chrome and Opera. Why?

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;
}

May be resize images in CSS, not JavaScript? Example:

img{
  width:200px;
  max-width:200px;
}
  • resized width and height automatically, without js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM