Home >Web Front-end >JS Tutorial >js function code to get image size_javascript skills

js function code to get image size_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:01:511833browse

1. Get the image size function getImageSize

Copy code The code is as follows:

function getImageSize(imageEl ) {
var i = new Image(); //Create a new image object
i.src = imageEl.src; //Assign the src attribute of the image to the src of the new image object
return new Array (i.width, i.height); //Return the length and width pixels of the image
//return [i.width, i.height];
}

2. Example
Copy code The code is as follows:

var imgEl = document.getElementById(imgEl),
imgSize = getImageSize(imgEl),
imgWidth = imgSize[0], //Get the width of the image
imgHeight = imgSize[1]; //Get the height of the image
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn