如何使用可调整大小的jQuery在iFrame中调整元素的大小?

| 我目前参与的一个项目有问题。这是我的情况。我正在动态构建网页并将其呈现到iframe,并且希望向用户提供在iframe中调整该呈现页面内的div元素大小的功能。要设置上下文,请考虑以下片段: 主机页面:
<html>
<head></head>
<body>
<div id=\"container\">
    <iframe id=\"site\" src=\"proxy.php\" style=\"width:100%;height:100%;\"></iframe>
</div>

<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js\"></script>
<script src=\"https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js\"></script>
<script src=\"http://jqueryui.com/ui/jquery.ui.resizable.js\"></script>
<script>
    $(\"#site\").load(function() {

        $(\"#site\").contents().find(\"#test-resize\").resizable({
            create: function(event, ui) {
                console.log(\"Create\");
            },
            start: function(event, ui) {
                console.log(\"Start\");
            },
            resize: function(event, ui) {
                console.log(\"Resize\");
            }
        });
    });
</script>
</body>
</html>
渲染页面:
<html>
<head>
<style>
    body { margin: 0; padding: 0; }
    #container { width: 960px;margin: auto;border: 1px solid #999; }
    header { display: block;background: #dedede;padding: 5px; }
    footer { display: block;padding: 10px;background: #555;color: #eee; }
    .ui-resizable { position: relative;}
    .ui-resizable-handle { position: absolute;font-size: 0.1px;z-index: 99999; display: block;}
    .ui-resizable-disabled .ui-resizable-handle, .ui-resizable-autohide .ui-resizable-handle { display: none; }
    .ui-resizable-n { cursor: n-resize; height: 7px; width: 100%; top: -5px; left: 0; }
    .ui-resizable-s { cursor: s-resize; height: 7px; width: 100%; bottom: -5px; left: 0; }
    .ui-resizable-e { cursor: e-resize; width: 7px; right: -5px; top: 0; height: 100%; }
    .ui-resizable-w { cursor: w-resize; width: 7px; left: -5px; top: 0; height: 100%; }
    .ui-resizable-se { cursor: se-resize; width: 12px; height: 12px; right: 1px; bottom: 1px; }
    .ui-resizable-sw { cursor: sw-resize; width: 9px; height: 9px; left: -5px; bottom: -5px; }
    .ui-resizable-nw { cursor: nw-resize; width: 9px; height: 9px; left: -5px; top: -5px; }
    .ui-resizable-ne { cursor: ne-resize; width: 9px; height: 9px; right: -5px; top: -5px;}
</style>
</head>
<body>
<div id=\"container\">
    <div id=\"main\">
        <header>
            <div><h1>Header</h1></div>
        </header>
    </div>
    <div id=\"body\">
        <h2>Body</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        <p>Mauris et sapien ligula, sit amet tincidunt ligula.</p>
    </div>
    <footer>
        <!-- I want the user to be able to resize this div -->
        <div id=\"test-resize\">Footer Div</div>
    </footer>
</div>
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js\"></script>
<script src=\"https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js\"></script>
<script src=\"http://jqueryui.com/ui/jquery.ui.resizable.js\"></script>
</body>
</html>
因此,宿主页面试图做的是进入呈现的页面并允许调整'test-resize \'元素的大小。我感到困惑的是,可调整大小的插件找到\'test-resize \'元素,然后添加必要的调整大小句柄div并触发\'create \'事件。但是,当尝试实际调整大小时,没有任何反应。没有任何变化,也没有事件触发。 有了这个,我的问题是,如何(如果有的话)进入iframe并调整元素的大小?请注意,这两个页面位于同一域中。 编辑:我想我应该澄清另一点。将调整大小代码添加到生成的页面时,调整大小可以正常工作,但是我想将代码保留在宿主页面级别。     
已邀请:
好吧,经过更多的研究,我提出了一个(最多被黑客入侵)的解决方案。不幸的是,它实际上涉及修改jQuery UI。请注意,我尚未对此结果进行彻底的分析来确定对性能(以及其他潜在指标)的影响。但是无论如何,为了使它起作用,需要更改几个位置。请注意,所有这些更改都是在UI Mouse小部件中的事件内发生的。当窗口小部件绑定mousemove和mouseup事件时,第一个更改是_mouseDown事件。更改:
$(document)
    .bind(\'mousemove.\'+this.widgetName, this._mouseMoveDelegate)
    .bind(\'mouseup.\'+this.widgetName, this._mouseUpDelegate);
至:
$($(this.element).get(0).ownerDocument)
    .bind(\'mousemove.\'+this.widgetName, this._mouseMoveDelegate)
    .bind(\'mouseup.\'+this.widgetName, this._mouseUpDelegate);
因此,这基本上将这些事件绑定到所有者文档,而不管所有者文档是常规的旧HTML页面还是iframe中的页面。其次,当用户放开鼠标时,我们需要取消绑定这些事件。在“ UI鼠标”小部件的_mouseUp事件中,更改:
$(document)
    .unbind(\'mousemove.\'+this.widgetName, this._mouseMoveDelegate)
    .unbind(\'mouseup.\'+this.widgetName, this._mouseUpDelegate);
至:
$($(this.element).get(0).ownerDocument)
    .unbind(\'mousemove.\'+this.widgetName, this._mouseMoveDelegate)
    .unbind(\'mouseup.\'+this.widgetName, this._mouseUpDelegate);
只需解除绑定我们上面绑定的事件即可。最后,我们需要返回_mouseDown事件并进行最终更改。在此功能的顶部,请注意以下行:
// don\'t let more than one widget handle mouseStart
if(mouseHandled) {return};
我们需要对此进行评论或将其删除。任意一个。之后,看来我们可以在iframe的内部和外部具有可调整大小的功能。是否破坏其他任何窗口小部件或插件,idk。但不要希望。如果大家都可以就此的潜在影响提出一些建议,并希望提供一个更优雅的解决方案,我将不胜感激。谢谢。     
我建议从“渲染页面”运行可调整大小。它是您的编辑器,将消除父级和iframe之间的所有调用。我发现从其他窗口操作元素很复杂,但并非总是最佳做法。相反,我一直将Windows之间的交互限制为对javascript函数的调用。我发现这样做可以更好地划分我的代码,并使其更容易移植到其他实例。     

要回复问题请先登录注册