我有一些问题,我目前正在参与一个项目。这是我的情况。我动态构建和渲染网页的iframe,而我希望提供用户调整该呈现的页面中的div元素在iframe的能力。要设置的背景下,考虑下面的代码片段:如何使用jQuery调整大小来调整iFrame中的元素大小?
主机页面:
<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'元素,然后添加必要的resize处理div并触发'create'事件。但是,当尝试实际调整大小时,没有任何反应。没有任何变化,没有事件发生。
就这样,我的问题是,我怎么能(如果有的话)伸进一个iframe和调整大小的元素?请注意,这两个页面位于同一个域中。
编辑:我想我应该澄清另一点。在将resize代码添加到生成的页面时,调整大小工作正常,但我希望将代码保留在主机页面级别。
太棒了!谢谢。 – 2012-07-18 09:24:10
我遵循这一点,我可调整大小的工作很好,但我的可拖动性是bug。 任何建议在此工作? – 2013-03-30 05:56:54