动态调整iframe的大小

jim*_*iny 13 html javascript css iframe jquery

情况: 我正在开发一个涉及典型HTML/CSS组合的响应式设计.除了在div中有iframe的情况之外,一切都很好用.iframe应自动调整为父div的大小.一个纯粹的css解决方案没有出现,所以我采用JQuery方法.它可以很好地工作,除了在一个场景中,从较小的宽度调整到较大的宽度屏幕.

HTML:

<div class="container">
    <iframe class="iframe-class" src="http://www.cnn.com/"></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.container {
    width: 100%;
    height: 250px;
}
.iframe-class {
    width: 100%;
    height: 100%;
    border: 1px solid red;
    overflow: auto;
}
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

$(function () {
    setIFrameSize();
    $(window).resize(function () {
        setIFrameSize();
    });
});

function setIFrameSize() {
    var ogWidth = 700;
    var ogHeight = 600;
    var ogRatio = ogWidth / ogHeight;

    var windowWidth = $(window).width();
    if (windowWidth < 480) {
        var parentDivWidth = $(".iframe-class").parent().width();
        var newHeight = (parentDivWidth / ogRatio);
        $(".iframe-class").addClass("iframe-class-resize");
        $(".iframe-class-resize").css("width", parentDivWidth);
        $(".iframe-class-resize").css("height", newHeight);
    } else {
        // $(".iframe-class-resize").removeAttr("width");
        // $(".iframe-class-resize").removeAttr("height");
        $(".iframe-class").removeClass("iframe-class-resize");
    }
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/TBJ83/

问题: 当窗口调整得更小时,它会不断检查窗口宽度,一旦达到<480 px,代码就会添加一个被调用的类iframe-class-resize,并将宽度和高度设置为该类.随着窗口调整大小,一旦大小达到480像素,它将删除该类.问题是设置width和height属性会将它们直接添加到元素而不是类本身.因此,删除类不会删除新的宽度和高度.我尝试使用removeAttr()(上面已注释掉)强制删除属性,但这不起作用.

有人看到上面的代码出错了吗?或者有关如何更有效地完成响应式iframe的任何建议?所需的主要内容是iframe必须位于内部,<div></div>而div可能不一定定义宽度或高度.理想情况下,父div应该明确定义宽度和高度,但是当前设置此站点的方式,这并不总是可行的.

附加: 如果上述内容不够清楚,请尝试以下方法重现该问题:

  • 在台式机上打开浏览器.我在Windows机器上使用Chrome.不要最大化浏览器.
  • 打开上面的jsfiddle(http://jsfiddle.net/TBJ83/).您会注意到iframe内容跨越了"预览"面板的整个宽度.
  • 手动调整宽度,直到整个窗口<480px.此时,iframe内容将非常小.
  • 手动调整宽度,直到整个窗口为>> 480px.目标是让iframe内容重新获得预览面板的整个宽度.相反,内容保留调整大小的宽度和高度,因为.css()函数将css更改直接应用于元素而不是类.

提前致谢!

Jas*_*per 9

您可以使用大约30个字符执行此操作.更改:

$(".iframe-class").removeClass("iframe-class-resize")
Run Code Online (Sandbox Code Playgroud)

至:

$(".iframe-class").removeClass("iframe-class-resize").css({ width : '', height : '' })
Run Code Online (Sandbox Code Playgroud)

这将重置您应用于元素的宽度/高度.使用时.css(),将传入的内容添加到style元素的属性中.传递空值时,jQuery会从style元素的属性中删除该属性.

这是一个更新的小提琴:http://jsfiddle.net/TBJ83/3/

编辑

好的,这里有一些针对性能的调整(以及其他一些方法):

$(function () {

    //setup these vars only once since they are static
    var $myIFRAME   = $(".iframe-class"),//unless this collection of elements changes over time, you only need to select them once
        ogWidth     = 700,
        ogHeight    = 600,
        ogRatio     = ogWidth / ogHeight,
        windowWidth = 0,//store windowWidth here, this is just a different way to store this data
        resizeTimer = null;

    function setIFrameSize() {
        if (windowWidth < 480) {

            var parentDivWidth = $myIFRAME.parent().width(),//be aware this will still only get the height of the first element in this set of elements, you'll have to loop over them if you want to support more than one iframe on a page
                newHeight      = (parentDivWidth / ogRatio);

            $myIFRAME.addClass("iframe-class-resize").css({ height : newHeight, width : parentDivWidth });
        } else {
            $myIFRAME.removeClass("iframe-class-resize").css({ width : '', height : '' });
        }
    }

    $(window).resize(function () {

        //only run this once per resize event, if a user drags the window to a different size, this will wait until they finish, then run the resize function
        //this way you don't blow up someone's browser with your resize function running hundreds of times a second
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(function () {
            //make sure to update windowWidth before calling resize function
            windowWidth = $(window).width();

            setIFrameSize();

        }, 75);

    }).trigger("click");//run this once initially, just a different way to initialize
});
Run Code Online (Sandbox Code Playgroud)


Abh*_*iya 6

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

iframe {
  height: 300px;
  width: 300px;
  resize: both;
  overflow: auto;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 太棒了 - 这就是我一直在寻找的东西,而且非常简单! (2认同)