如何调整 iframe 的大小?

发布于 2024-12-15 23:02:07 字数 143 浏览 2 评论 0原文

我们有这个小组项目,我被分配去调整某个 iframe 的大小。自上周以来,我阅读了大量论坛帖子,发现 iframe 本身无法调整大小。

有没有办法让我的 iframe 调整大小?

We have this group project and I was assigned to make a certain iframe resizable. I've been reading lots of forum posts since last week, and I found out that iframe itself can't be resizable.

Is there a way to make my iframe resizable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

看透却不说透 2024-12-22 23:02:07

这可以用纯 CSS 来完成,如下所示:

iframe {
  height: 300px;
  width: 300px;
  resize: both;
  overflow: auto;
}

将高度和宽度设置为您想要的最小尺寸,它看起来只会变大,不会缩小。

实例:https://jsfiddle.net/xpeLja5t/

这种技术有对除 IE 之外的所有主流浏览器都有良好的支持

This can be done in pure CSS, like so:

iframe {
  height: 300px;
  width: 300px;
  resize: both;
  overflow: auto;
}

Set the height and width to the minimum size you want, it only seems to grow, not shrink.

Live example: https://jsfiddle.net/xpeLja5t/

This technique has good support in all major browsers except IE.

最后的乘客 2024-12-22 23:02:07

我花了一个多小时找到一些东西,这让我找到了一个可行的解决方案:

  • 纯CSS,因为它应该是
  • 对于所有现代完全支持 Flexbox 的浏览器,例如 Firefox、Chrome 等。
  • 不过,不适用于 IE10 及更低版本等较旧的浏览器
.resizer { display:flex; margin:0; padding:0; resize:both; overflow:hidden }
.resizer > .resized { flex-grow:1; margin:0; padding:0; border:0 }

.ugly { background:red; border:4px dashed black; }
<div class="resizer ugly">
  <iframe class="resized" src="https://wikipedia.org/"></iframe>
</div>

在 Chrome 中,这个解决方案仍然存在一些问题。使用运行代码片段,然后整页(因此,如果IFRAME嵌入到其他IFRAMEs)。此外,调整大小手柄(位于右下角)显示得有点微妙,调整大小通常会随着鼠标停止。

在 Firefox 中,您现在可以使用 pure 调整 iframe 的大小CSS 也是如此。

维基百科是我发现的唯一一个还没有针对 iframe 嵌入的 CSP 的 URL。如果他们添加了一个,iframe 就会变成空,并且您的浏览器控制台将记录 CSP 错误。在这种情况下,请使用一些有效的 URL 更新我的答案。谢谢。

It took more than 1 hour to find something which led me to a working solution:

  • Pure CSS as it should be
  • For all modern browsers with full flexbox support, like Firefox, Chrome, etc.
  • Does not work for older browsers like IE10 and below, though

.resizer { display:flex; margin:0; padding:0; resize:both; overflow:hidden }
.resizer > .resized { flex-grow:1; margin:0; padding:0; border:0 }

.ugly { background:red; border:4px dashed black; }
<div class="resizer ugly">
  <iframe class="resized" src="https://wikipedia.org/"></iframe>
</div>

In Chrome this solution still has some issues at my side. Use Run code snippet and then Full Page (so it does not seem to work if the IFRAME is embedded in other IFRAMEs). Also the resize handle (in the right bottom corner) is shown a bit subtle, and resizing often stops to follow the mouse.

In Firefox you can resize the iframe now using pure CSS, too.

Wikipedia was the only URL I found which has no CSP against iframe-embedding yet. If they ever add one, the iframe becomes empty and your Browser's console will log the CSP error. In this case, please update my answer with some working URL. Thanks.

咋地 2024-12-22 23:02:07

这些步骤将使 iframe 可调整大小:

  1. 创建一个用于调整大小的 div。例如:

    ;
  2. 将 style 标签应用于 div 的样式。

    .ui-可调整大小-helper {
        边框:1px 灰色虚线;
    }
    .可调整大小{
        显示:块;
        宽度:400px;
        高度:400像素;
        内边距:30px;
        边框:2px 纯灰色;
        溢出:隐藏;
        位置:相对;
    }
    内联框架{
        宽度:100%;
        高度:100%;
    }
    
  3. 包括 jQuery 和jQuery 用户界面

    
    <脚本 src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
    <脚本 src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">
    
  4. 执行可调整大小

    $(function () {
        $(".ressized").ressized({
            动画:真实,
            animateEasing: '摇摆',
            动画持续时间:500
        });
    });
    

These steps will make an iframe resizable:

  1. Create a div for resizing. eg:

    <div class="resizable"></div>
    
  2. Apply the style tag for style of div.

    .ui-resizable-helper {
        border: 1px dotted gray;
    }
    .resizable {
        display: block;
        width: 400px;
        height: 400px;
        padding: 30px;
        border: 2px solid gray;
        overflow: hidden;
        position: relative;
    }
    iframe {
        width: 100%;
        height: 100%;
    }
    
  3. Include jQuery & jQuery UI

    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/start/jquery-ui.css"rel="stylesheet" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    
  4. Execute Resizable

    $(function () {
        $(".resizable").resizable({
            animate: true,
            animateEasing: 'swing',
            animateDuration: 500
        });
    });
    
吻安 2024-12-22 23:02:07

使用 jQuery UI...

HTML:

<div class="resizable" style="width: 200px; height: 200px;">
    <iframe src="http://www.example.com/" style="width: 100%; height: 100%;"></iframe>
</div>

JavaScript:

$(".resizable").resizable({
    start: function(event, ui) {
        ui.element.append($("<div/>", {
            id: "iframe-barrier",
            css: {
                position: "absolute",
                top: 0,
                right: 0,
                bottom: 0,
                left: 0,
                "z-index": 10
        }
        }));
    },
    stop: function(event, ui) {
        $("#iframe-barrier", ui.element).remove();
    },
    resize: function(event, ui) {
        $("iframe", ui.element).width(ui.size.width).height(ui.size.height);
    }
});

jsFiddle: http://jsfiddle.net/B5vHQ/97/

说明:

由于 jQuery UI 的 ressized 插件无法直接在 iframe 上工作,因此将 iframe 包装在 div 中并调整 div 的大小 反而。上面代码中的 resize 事件确保 iframe 调整大小以匹配 div。

然而,此方法还有一个额外的问题 - iframe 不会将鼠标移动事件传递给其父元素。为了解决这个问题,startstop 事件在调整大小期间用另一个元素覆盖 iframe,以便 ressized 插件可以跟踪鼠标动作正确。

出于某种原因,必须为包装器 div 指定明确的大小才能使调整大小起作用。在大多数情况下,这意味着 iframe 需要设置相同的大小,以便它从一开始就与 div 的大小相匹配。

With jQuery UI...

HTML:

<div class="resizable" style="width: 200px; height: 200px;">
    <iframe src="http://www.example.com/" style="width: 100%; height: 100%;"></iframe>
</div>

JavaScript:

$(".resizable").resizable({
    start: function(event, ui) {
        ui.element.append($("<div/>", {
            id: "iframe-barrier",
            css: {
                position: "absolute",
                top: 0,
                right: 0,
                bottom: 0,
                left: 0,
                "z-index": 10
        }
        }));
    },
    stop: function(event, ui) {
        $("#iframe-barrier", ui.element).remove();
    },
    resize: function(event, ui) {
        $("iframe", ui.element).width(ui.size.width).height(ui.size.height);
    }
});

jsFiddle: http://jsfiddle.net/B5vHQ/97/

Explanation:

Since jQuery UI's resizable plugin won't work on an iframe directly, wrap the iframe in a div and resize the div instead. The resize event in the code above ensures that the iframe resizes to match the div.

There's an additional problem with this method, however - iframes don't pass mouse movement events to their parent element. To work around that, the start and stop events cover the iframe up with another element during the resize, so that the resizable plugin can track mouse movement correctly.

For some reason, giving the wrapper div an explicit size is necessary for the resizing to work. In most cases, that means that the iframe needs the same size set, so thatit matches the size of the div right from the start.

感情废物 2024-12-22 23:02:07

如果您还没有找到解决方案,我已经成功:

Jquery:

$("#my-div-frame-wrapper").resizable({
    alsoResize : '#myframe'
});

HTML:

<div id="my-div-frame-wrapper">
    <iframe id="myframe"></iframe>
</div>

我希望它有帮助

In case you haven't found the solution yet, I've had success with:

Jquery:

$("#my-div-frame-wrapper").resizable({
    alsoResize : '#myframe'
});

HTML:

<div id="my-div-frame-wrapper">
    <iframe id="myframe"></iframe>
</div>

I hope it helps

掀纱窥君容 2024-12-22 23:02:07

我找到了您的解决方案:

HTML:

<div id="resizable" class="ui-widget-content" 
     style="border-style:solid; height:400px; width:400px">
    <h3 class="ui-widget-header">Resizable</h3>
    <iframe src="test.aspx" id="myfr" scrolling="no"
            frameborder="0" marginheight="0" marginwidth="0" >
        Your Browser Do not Support Iframe
    </iframe>
</div>

JQUERY:

$(function() {
    $("#resizable").resizable();

    $("#resizable").resizable({
        resize: function(event, ui) {
            $("#myfr").css({ "height": ui.size.height,"width":ui.size.width});
        }
    });
});

I found your solution:

HTML:

<div id="resizable" class="ui-widget-content" 
     style="border-style:solid; height:400px; width:400px">
    <h3 class="ui-widget-header">Resizable</h3>
    <iframe src="test.aspx" id="myfr" scrolling="no"
            frameborder="0" marginheight="0" marginwidth="0" >
        Your Browser Do not Support Iframe
    </iframe>
</div>

JQUERY:

$(function() {
    $("#resizable").resizable();

    $("#resizable").resizable({
        resize: function(event, ui) {
            $("#myfr").css({ "height": ui.size.height,"width":ui.size.width});
        }
    });
});
吃→可爱长大的 2024-12-22 23:02:07

我发现的最佳解决方案是将 iframe 的宽度和高度设置为 100% 并将 iframe 放在 div 标签内。这是您使用 CKEditor 等工具看到的基本解决方案。

例如,我有一些 jQuery 对话框在 Utah's Disclosures Site。如果单击 or 的报告或语句,您将看到弹出一个对话框,其中包含 iframe。希望有帮助。

The best solution I've found is to set the width and height of the iframe to 100% and put the iframe within a div tag. This is the basic solution you see with tools like CKEditor.

For an example, I have some jQuery dialogs that do just that on the Utah's Disclosures Site. If you click on a report or statement of or, you'll see a dialog that pops up with an iframe in it. Hope it helps.

分分钟 2024-12-22 23:02:07

非常简单,

首先 -

添加 jquery 和 jquery-ui -

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

然后 -

<script>
$(function() {
$( "#resizable" ).resizable();
});
</script>

最后 -

<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Resizable</h3>
</div>

完成!

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Resizable - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style>
#resizable { width: 150px; height: 150px; padding: 0.5em; }
#resizable h3 { text-align: center; margin: 0; }
</style>
<script>
$(function() {
$( "#resizable" ).resizable();
});
</script>
</head>
<body>
<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Resizable</h3>
</div>
</body>
</html>

表单 http://jqueryui.com/ressized/

very easy,

first -

add jquery and jquery-ui -

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

then -

<script>
$(function() {
$( "#resizable" ).resizable();
});
</script>

last -

<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Resizable</h3>
</div>

Its done!

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Resizable - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style>
#resizable { width: 150px; height: 150px; padding: 0.5em; }
#resizable h3 { text-align: center; margin: 0; }
</style>
<script>
$(function() {
$( "#resizable" ).resizable();
});
</script>
</head>
<body>
<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Resizable</h3>
</div>
</body>
</html>

form http://jqueryui.com/resizable/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文