Home > Web Front-end > JS Tutorial > How to get the real size of an image in jquery

How to get the real size of an image in jquery

coldplay.xixi
Release: 2020-11-30 17:02:01
Original
2785 people have browsed it

How to get the true size of the image in jquery: 1. Method 1 [.attr("src", $(''#imgid").attr("src"))]; 2. Method 2 [ theImage.src = $(''#imgid").attr( "src");].

How to get the real size of an image in jquery

The operating environment of this tutorial: windows10 system, jquery2.2.4, this article is applicable to all brands of computers.

How to get the true size of the image in jquery:

The first one:

$("<img/>") // Make in memory copy of image to avoid css issues  在内存中创建一个img标记
    .attr("src", $(&#39;&#39;#imgid").attr("src"))
    .load(function() {
        pic_real_width = this.width;   // Note: $(this).width() will not
        pic_real_height = this.height; // work for in memory images.
    });
Copy after login

The second one:

var theImage = new Image(); 
theImage.src = $(&#39;&#39;#imgid").attr( "src"); 
alert( "Width: " + theImage.width); 
alert( "Height: " + theImage.height);
Copy after login

One is more secure and is guaranteed to be obtained after the image is loaded.

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to get the real size of an image in jquery. For more information, please follow other related articles on the PHP Chinese website!

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