Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Draggable and Resizeable over IFrames (Solution)

Tags:

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);
like image 549
Bob Avatar asked Sep 02 '10 12:09

Bob


People also ask

Why is draggable not working?

You have one of these problems: Your jQuery or jQuery UI Javascript path files are wrong. Your jQuery UI does not include draggable. Your jQuery or jQuery UI Javascript files are corrupted.


2 Answers

Try this:

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

This should work.

like image 141
vishal Avatar answered Oct 19 '22 10:10

vishal


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.

like image 44
stt Avatar answered Oct 19 '22 10:10

stt