JQuery 通过 IFrame 进行可拖动和可调整大小(解决方案)

发布于 2024-09-17 01:32:11 字数 1560 浏览 4 评论 0原文

我最近在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝天 2024-09-24 01:32:12

为什么这么难?还有更漂亮的解决方案:

  • 将你的 iframe 放在带有一些 z-index 的相对 div 中,比如 0
  • 也使 iframe 相对,并在拖动过程中将其 z-index 更改为 -1:

代码:

$("#Div").draggable({
    start: function () {
        $("iframe").css('z-index', '-1');
    },
    stop: function () {
        $("iframe").css('z-index', '0');
    }
});

Why so hard? There is much more beautiful solution:

  • put your iframe inside relative div with some z-index, say 0
  • make iframe relative also and change it's z-index to -1 during dragging:

code:

$("#Div").draggable({
    start: function () {
        $("iframe").css('z-index', '-1');
    },
    stop: function () {
        $("iframe").css('z-index', '0');
    }
});
烟柳画桥 2024-09-24 01:32:12

所有的答案都很好,但对我来说没有一个可以轻松实现!

我得到了帮助,但最后,我使用了:

Inside the Iframe Header

var iframes = window.parent.$('iframe');
iframes.each(function () {
    if ($(this).attr('src') == '<YOUR SOURCE PATH>')
    $(this).css('z-index', '-1');
});

这是为了修复 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:

var iframes = window.parent.$('iframe');
iframes.each(function () {
    if ($(this).attr('src') == '<YOUR SOURCE PATH>')
    $(this).css('z-index', '-1');
});

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!

毁梦 2024-09-24 01:32:11

试试这个:

 $('#Div').draggable({ iframeFix: true });

这应该可行。

Try this:

 $('#Div').draggable({ iframeFix: true });

This should work.

骄傲 2024-09-24 01:32:11

我所做的是定义 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 add dragging 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.

柳絮泡泡 2024-09-24 01:32:11

有多种方法可以实现这一目标,具体取决于您的需求。我发现调整大小/拖动许多窗口会大大降低 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.

小瓶盖 2024-09-24 01:32:11

我首先也喜欢 Byron Cobb 的解决方案,但由于我使用的是对话框元素,并且显示对话框时不需要 Iframe(它是一个保存对话框),所以我喜欢使用 modal 选项:

$('#savingDialog').dialog({
    modal: true
});

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:

$('#savingDialog').dialog({
    modal: true
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文