我有一些問題,我目前正在參與一個項目。這是我的情況。我動態構建和渲染網頁的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