How do I get the original size of a picture in JavaScript on the browser side?

Source: Internet
Author: User

var img = $ ("#img_id"); Get my img elemvar pic_real_width, pic_real_height;$ ("

Technically, we can easily limit the picture with the maximum width of the text, so that all of them remain one width, without the width of the text, we need each picture's own size. We can declare the original size of the picture when the server is edited. A more flexible way is to dynamically get the original size of the image by placing a section of JS on the page, dynamically changing the image's display size. This is compatible with the old and also the maximum width of the text way, you can also let the picture when needed to show its original size.

WebKit Browser (Google Chrome, etc.) is the image of the loaded event before you get the height and width values. So, you can't delay waiting with the timeout function, the best way is to use the OnLoad event of the picture.

To avoid the effect of CSS on the size and size of a picture, the above code copies the image into memory for calculation.

If your page is an old-fashioned page, you can embed the code on the bottom of the page as needed, and it doesn't require you to modify the original page.

How do I get the original size of a picture in JavaScript on the browser side?

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.