Home > Web Front-end > JS Tutorial > body text

Highly adaptive code when iframe src is an image_javascript skills

WBOY
Release: 2016-05-16 19:08:22
Original
1295 people have browsed it

The issue of iframe height adaption has been discussed n times on the Internet. It is generally achieved by accessing the offsetHeight and offsetWidth of the body of the iframe when the iframe is loaded, and then updating the height and width of the iframe.
The adaptive problem encountered today is a little different from the past. This time the src is a picture, and it is a picture of uncertain size (generated from the data taken from the database). Here we use the previous The method doesn't seem to be that effective. It suddenly occurred to me that the height and width of the img tag can be obtained. Then suddenly I had an idea.

The solution is as follows:
First dynamically generate an img element, assign its src to the img tag when the iframe page is loaded (onload), then obtain the height and width of the img, and then dynamically modify the iframe height Width is enough, and don’t forget to set the margin and padding of the iframe page body to 0, otherwise scroll bars will appear because padding and margin are not 0 by default.


[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]
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