JQuery 通过 IFrame 进行可拖动和可调整大小(解决方案)
我最近在使用 JQuery Draggable 和 Resizing 插件时遇到了一些麻烦。在寻找解决方案时,我在许多不同的地方发现了一些非常零碎的代码,最后归档到一个干净的解决方案,该解决方案似乎对我来说几乎完美。
我想我会与其他人分享我的发现,如果他们也遇到这个问题的话。
我有一个包含 IFrame 的 div。该 div 必须可调整大小且可拖动。足够简单 - 将 jquery 可拖动和可调整大小添加到 div 中,如下所示:
$("#Div").draggable();
$("#Div").resizable();
一切都很好,直到您拖动包含 iframe 的另一个 div 或尝试通过在当前 iframe 上移动来调整当前 div 的大小。当超过 iframe 时,这两个函数都会停止。
解决方案:
$("#Div").draggable({
start: function () {
$(".AllContainerDivs").each(function (index, element) {
var d = $('<div class="iframeCover" style="zindex:99;position:absolute;width:100%;top:0px;left:0px;height:' + $(element).height() + 'px"></div>');
$(element).append(d);});
},
stop: function () {
$('.iframeCover').remove();
}
});
$("#Div").resizable({
start: function () {
$(".AllContainerDivs").each(function (index, element) {
var d = $('<div class="iframeCover" style="z-index:99;position:absolute;width:100%;top:0px;left:0px;height:' + $(element).height() + 'px"></div>');
$(element).append(d);
});
},
stop: function () {
$('.iframeCover').remove();
}
});
享受吧!
PS:一些额外的代码来创建窗口,当选择这些窗口时,这些窗口将被带到其他可拖动对象的前面:
在可拖动开始函数中 -
var maxZ = 1;
$(".AllContainerDivs").each(function (index, element) {
if ($(element).css("z-index") > maxZ) {
maxZ = $(element).css("z-index");
}
});
$(this).css("z-index", maxZ + 1);
I recently ran into some troubles using JQuery Draggable and Resizable plugins. Looking for solutions, i found some very fragmented code in many different places and finally filed down to a clean solution which seems to work almost perfectly for me.
I thought i'd share my findings for anyone else, should they come accross this issue too.
I have a div which contains and IFrame. This div must be resizeable and draggable. Simple enough - add the jquery draggable and resizable to the div like so:
$("#Div").draggable();
$("#Div").resizable();
All is fine until you drag over another div containing an iframe or try to resize your current div, by moving over your current iframe. Both functions stop when over an iframe.
Solution:
$("#Div").draggable({
start: function () {
$(".AllContainerDivs").each(function (index, element) {
var d = $('<div class="iframeCover" style="zindex:99;position:absolute;width:100%;top:0px;left:0px;height:' + $(element).height() + 'px"></div>');
$(element).append(d);});
},
stop: function () {
$('.iframeCover').remove();
}
});
$("#Div").resizable({
start: function () {
$(".AllContainerDivs").each(function (index, element) {
var d = $('<div class="iframeCover" style="z-index:99;position:absolute;width:100%;top:0px;left:0px;height:' + $(element).height() + 'px"></div>');
$(element).append(d);
});
},
stop: function () {
$('.iframeCover').remove();
}
});
Enjoy!
PS: Some extra code to create windows which, when selected, are brought to the front of the other draggables:
In the draggable start function -
var maxZ = 1;
$(".AllContainerDivs").each(function (index, element) {
if ($(element).css("z-index") > maxZ) {
maxZ = $(element).css("z-index");
}
});
$(this).css("z-index", maxZ + 1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为什么这么难?还有更漂亮的解决方案:
代码:
Why so hard? There is much more beautiful solution:
code:
所有的答案都很好,但对我来说没有一个可以轻松实现!
我得到了帮助,但最后,我使用了:
Inside the Iframe Header
tag:
这是为了修复 TinyMCE IFrame Draggable 问题。在 iframe 中应用此代码时,出现不可拖动问题。另外,检查 *.js 文件的顺序,它会有所不同!
All great answers, but none were easily implemented for me!
I was helped, but in the end, I used:
Inside the Iframe Header
<script>
tag:This is to fix a TinyMCE IFrame Draggable issue. on applying this code inside the iframe, the Non-Draggable problem. Also, check the order of the *.js files, it makes a difference!
试试这个:
这应该可行。
Try this:
This should work.
我所做的是定义
body.dragging iframe {pointer-events: none;}
然后将dragging
类添加到 Dragstart 事件上的 body 并在 Dragend 事件上将其删除。对我来说效果很好,不知道为什么之前没有提到过,据我所知,pointer-events CSS 属性在 2010 年就已经存在了。
What I've done is define
body.dragging iframe {pointer-events: none;}
then adddragging
class to body on dragstart event and remove it on dragend event.Works fine for me, not sure why it wasn't mentioned before, as far as I can tell pointer-events CSS property was already around in 2010.
有多种方法可以实现这一目标,具体取决于您的需求。我发现调整大小/拖动许多窗口会大大降低 UI 速度,因此我最终在调整大小/拖动开始时隐藏了 iframe,并带有边框以帮助导航。
有一些 jquery 插件可以实现部分此功能,但许多插件都难以使用 iframe。
前置也可以在某些方面进行改进,但可能并不适用于所有情况。
There are a number of ways to achieve this, all depending on your needs. I found resizing/dragging many windows slows the UI down a lot, and as such I ended up hiding the iframes on start of resize/Drag with a border to help navigation.
There are some jquery plugins that achieve part of this functionality, but many struggle with iframes.
The bring to front can also be improved at points and may not work in all situations.
我首先也喜欢 Byron Cobb 的解决方案,但由于我使用的是对话框元素,并且显示对话框时不需要 Iframe(它是一个保存对话框),所以我喜欢使用 modal 选项:
I first also went like Byron Cobb's solution, but as I'm using a dialog element and the Iframe isn't needed when the dialog is shown (it's a saving dialog), I liked using the modal option: