Home >Web Front-end >JS Tutorial >Highly adaptive code when iframe src is an image_javascript skills
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.