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

js function code to get image size_javascript skills

WBOY
Release: 2016-05-16 18:01:51
Original
1791 people have browsed it

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
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template