可以在不重新加载框架的情况下调整 iframe 的大小
It is possible to Resize an iframe without reloading the frame?
嗨堆栈溢出社区。我有一个问题。我有一个使用iFrame的过程。这个过程有一些Javascript来根据iFrame内容设置iFrame高度。我认为这还不够,所以我决定根据内容更改自动调整 iFrame 高度的大小,而无需重新加载任何 iFrame 或进程。不幸的是,我尝试过,但由于怀疑而无法做到。我在堆栈溢出中到处搜索,但没有找到解决方案。
规则:
- 在 iFrame 中自动调整高度大小。
- 不会在父窗口或 iFrame 中重新加载。 没有
- 触发器,没有间隔。
- 一切都是自动的。
窗口和 iFrame 位于同一域中。
你能帮帮我吗?谢谢,很抱歉英语不好。
我的实际代码:
if(document.getElementById("frame") != undefined)
{
var frame = document.getElementById("frame");
$(frame).load(function(){
var frame_height = frame.contentWindow.document.body.scrollHeight + 'px';
frame.setAttribute('style', 'height:' + frame_height + ' !important');
console.log('its this loading?');
contentBasedIframeResize(); //this is worthless, just testing. isn't dynamic right now. need fix.
// As soon as the frame finish the load, this frame height will be based in the frame content
});
function contentBasedIframeResize()
{
var add_button = $(frame).contents().find(".some_obj"); //preparing the trigger
var remove_button = $(frame).contents().find(".some_obj"); //preparing the trigger
if(add_button != undefined)
{
$(add_button).click(function(){
var add_height = frame.contentWindow.document.body.scrollHeight + 'px';
frame.setAttribute('style', 'height:' + add_height + ' !important');
});
}
if(remove_button != undefined)
{
$(remove_button).click(function(){
var remove_height = $("#frame").contents().find("#table_obj").height();
remove_height = parseInt(remove_height) + 100; //hardcoded
frame.setAttribute('style', 'height:' + remove_height + 'px !important');
console.log("its this working v2?");
});
}
}
}
试试这个,
function resizeIframe(obj)
{
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
$('iframe').load(function(){
resizeIframe(this);
});
相关文章:
- 如何在未直接触发的情况下停止事件
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 在不使用jquery的情况下查找页面中的所有锚点并附加函数
- 我可以在不使用整个 Meteor 或 Blaze 框架的情况下使用 Minimongo 吗?
- 如何使用AJAX函数在不刷新页面的情况下发送表单,我缺少什么?我必须为此使用休息框架吗?
- 简单的淡入和淡出效果在没有框架的情况下点击
- 可以在不重新加载框架的情况下调整 iframe 的大小
- 使用 AJAX 从数据库中检索数据,并在没有框架的情况下对它们进行分页
- 手风琴导航可以在没有框架的情况下使用吗
- 在没有框架的情况下构建HTML5游戏
- 在没有实体框架的情况下使用Breeze.js
- 在不重新加载页面的情况下发布请求,并且不支持javascript(Node Express框架)
- imacros:在不按下按钮的情况下,“查找框架”按钮处于
- 是否有一个服务器端JavaScript框架(node.js)可以在不使用模板引擎的情况下生成html页面
- 如何在不使用HTML框架的情况下做到这一点
- 如何在不复制框架的情况下创建几个ExtJs应用程序?
- 如何在不破坏浏览器后退按钮的情况下跳出框架?
- phoenix框架-如何在不重新启动服务器的情况下更改Javascript
- 如何在不使用任何Javascript框架的情况下实现MV*架构?
- 开始在没有框架的情况下进行JavaScript单元测试