扫码关注官方订阅号
光阴似箭催人老,日月如移越少年。
$(function() { // 自适应iframe高度 if (window.frameElement) { var paddingPenalty = 60; var containerMinHeight = 1000; var $window = $(window); var $body = $(window.document.body); var $parentFrame = $(window.frameElement); var $parentFrameContainer = $parentFrame.parent(); var parentFrameContainerOriginHeight = $parentFrameContainer.height(); var parentFrameHeight = parentFrameContainerOriginHeight + paddingPenalty; // 自适应parent frame高度的function var resizeParentFrame = function(newHeight) { if ($parentFrame.is(':hidden')) { return; } if (parent.window.frameElement) { var $nestedParentFrame = $(parent.window.frameElement); var $nestedParentFrameContainer = $nestedParentFrame.parent(); var nestedParentFrameHeight = $nestedParentFrameContainer.outerHeight(); if ($nestedParentFrame.is(':hidden')) { return; } var tempHeight = newHeight; tempHeight += paddingPenalty; if (parentFrameHeight && (parentFrameHeight < nestedParentFrameHeight)) { $nestedParentFrameContainer.css('height', tempHeight <= parentFrameHeight ? parentFrameHeight : tempHeight); } else { $nestedParentFrameContainer.css('height', tempHeight <= nestedParentFrameHeight ? nestedParentFrameHeight : tempHeight); } } newHeight += paddingPenalty; $parentFrameContainer.css('height', newHeight <= containerMinHeight ? containerMinHeight : newHeight); } // iframe关闭的时候,取消iframe的高度 $window.unload(function() { $parentFrameContainer.css('height', parentFrameContainerOriginHeight); }); // 页面加载的完毕后,调整iframe高度 //resizeParentFrame($body.height()); resizeParentFrame(containerMinHeight); // 每隔500ms侦测body实际内容高度是否发生变化 var checkBodyHeight = function(){ var lastHeight = containerMinHeight, newHeight, timer; (function run(){ if (!$parentFrame.is(':hidden')) { newHeight = $body.height(); if (newHeight > parseInt($parentFrame.parent().css("height").replace("px", ""))) { $parentFrame.parent().css("height", newHeight); } if( newHeight > lastHeight ) { resizeParentFrame(newHeight); } lastHeight = newHeight + paddingPenalty; } timer = setTimeout(run, 200); })(); } checkBodyHeight(); } });
//html <p id="iframeBox"> <iframe src=""></iframe> </p> //js $('#iframeBox').find('iframe').attr('src',src); $('#iframeBox').find('iframe').load(function(){ //iframe框架页面正常高度 var height=$(this).contents().find('body').height(); //iframeBoxHeight固定为780px var iframeBoxHeight=780; if(height > iframeBoxHeight){ $(this).height(height); }else{ $(this).height(iframeBoxHeight); } });
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
光阴似箭催人老,日月如移越少年。