JavaScript get picture size and enlarge picture _javascript tips

Source: Internet
Author: User
1) Get picture size
Copy Code code as follows:

<script>
function Getwh (t) {
Dom Properties
Console.log ("width=" +t.width);//200
Console.log ("height=" +t.height);//300
Action Style
Console.log ("stylewidth=" +t.style.width);
Console.log ("styleheight=" +t.style.height);
}

2 Get picture size (not set width height)
Copy Code code as follows:


<script>
function Getwh (t) {
Dom Properties
Console.log ("width=" +t.width);//200
Console.log ("height=" +t.height);//300
Action Style
Console.log ("stylewidth=" +t.style.width);
Console.log ("styleheight=" +t.style.height);
}

As long as we do not explicitly set it in style, the width of the height is always empty!

3) Enlarge the picture

Here we use IE's private property to prevent image amplification distortion serious!
Copy Code code as follows:

<script>
function Getwh (t) {
T.width *= 2;
T.height *= 2;
Once per click, the width and height are magnified one times
}
</script>

4 in FF and Google, we can also use Naturalwidth and naturalheight to get the original size of the picture!
Copy Code code as follows:

<script>
function Getwh (t) {
Console.log ("width=" +t.naturalwidth);
Console.log ("height=" +t.naturalheight);
T.width = T.naturalwidth * 2;
T.height = T.naturalheight * 2;
}
</script>

Naturalwidth and Naturalheight are only read-only properties and cannot be used to set the size of the picture and cannot be magnified continuously.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.