Created
March 27, 2014 09:28
-
-
Save zmmbreeze/9803690 to your computer and use it in GitHub Desktop.
使同域的iframe宽度撑满其所在容器,并且高度自适应其内容
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 使同域的iframe宽度撑满其所在容器,并且高度自适应其内容 | |
* | |
* @param {Element} iframe . | |
*/ | |
function autoFitIframe(iframe) { | |
var doc = iframe.contentDocument || iframe.contentWindow.document; | |
// 设置iframe宽度 | |
iframe.style.width = '100%'; | |
function update() { | |
var containerWidth = iframe.parentNode.offsetWidth; | |
// 在iphone、ipad等移动浏览器中,为iframe设置width和height样式起不了作用 | |
// iframe的高宽由其内容决定,故设置iframe中body的宽度来限制iframe高宽 | |
doc.body.style.width = + 'px'; | |
doc.body.style.padding = '0'; | |
doc.body.style.margin = '0'; | |
doc.body.style.border = 'none'; | |
// 自适应iframe高度,确保没有纵向滚动条 | |
// iphone、ipad等移动浏览会器忽略width/height自适应高度 | |
// NOTE: 没有支持Quirks mode | |
// 确保scrollHeight是iframe所需的最小高度 | |
iframe.style.height = '0'; | |
iframe.style.height = Math.max( | |
// 其他浏览器 | |
doc.body.scrollHeight, | |
// IE7 | |
doc.documentElement.scrollHeight | |
) + 'px'; | |
} | |
if (doc.readyState === 'complete') { | |
update(); | |
} | |
if (iframe.addEventListener) { | |
iframe.addEventListener('load', update, false); | |
} | |
else if (iframe.attachEvent) { | |
iframe.attachEvent('onload', update); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment