IFRAME adaptive height

Source: Internet
Author: User

Recently, I encountered the IFRAME adaptive height problem.

If the height attribute is specified in IFRAME and the content height in IFRAME is smaller than the given height, a drop-down box is generated in the mobile browser inexplicably, resulting in a decrease in experience.

However, if you do not set the height attribute, the IFRAME height is always kept at 150px. In this way, we can only dynamically change the IFRAME height through js to achieve highly adaptive IFRAME.


The essence of highly adaptive is to set the IFRAME height through the content height.

However, because the background color of the content in IFRAME is different from that of the parent page, we use the IFRAME height when the content height is smaller than the screen height.

Therefore, we can determine the final display height of our IFRAME by comparing the screen height and the content height in IFRAME.

The IFRAME code on the parent page is as follows:

<iframe allowtransparency="true" id="content" name="content" src="xxx.html"  scrolling="no" frameborder="0" ></iframe>

Code for obtaining the screen size:

Function getscreensize (winobj) {var size = {width: 0, height: 0} // obtain the window width if (winobj. innerwidth) {size. width = winobj. innerwidth;} else if (winobj.doc ument. body) & (winobj.doc ument. body. clientwidth) size. width = winobj.doc ument. body. clientwidth; // obtain the window height if (winobj. innerheight) size. height = winobj. innerheight; else if (winobj.doc ument. body) & (winobj.doc ument. body. clientheight) size. height = winobj.doc ument. body. clientheight; // detects the body in the document to obtain the window size. If (winobj.document.doc umentelement & winobj.document.doc umentelement. clientheight & winobj.document.doc umentelement. clientwidth) {size. height = winobj.document.doc umentelement. clientheight; size. width = winobj.document.doc umentelement. clientwidth;} return size ;}


Because the IFRAME must be highly adaptive every time the content in the IFRAME changes, my adaptive code is added to the page in the IFRAME and runs after the page load ends:

Window. onload = function () {// obtain the Frame Node var contentframe = parent.doc ument in the parent page. getelementbyid ('content'); // obtain the screen height var parentscrheight = getscreensize (window. parent ). height; // obtain the height of your own content var contentheight = document. body. clientheight; // If the screen is high, use the frame content height. // if the frame content is high, use the screen height contentframe. style. height = parentscrheight <contentheight? Contentheight + "PX": parentscrheight + "PX ";};

The above steps complete the IFRAME adaptive control function through Js.




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.