Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Resizable and Draggable elements from collapsing on each other?

Hey all. I have the following code:

http://jsfiddle.net/g7Cgg/

As you can see, there are 2 simple DIVs that are stacked one above each other. Each of these DIVs are also set to be resizable and draggable. However, notice when you attempt to resize the first element, the second element collapses onto the first. From what I can see, this is because resizable changes the element to a position of absolute. How can I prevent this behavior? Is it possible to resize the element while retaining the ability to drag the element?

Note also, that if you change the elements to have a position of relative (add position:relative !important in the .demo style), it sort of prevents the collapsing, but the element "jumps" when you begin to resize or drag. Another weird behavior. Thanks for your help.

like image 566
naivedeveloper Avatar asked May 17 '11 21:05

naivedeveloper


People also ask

How do I disable draggable?

We can disable drag and drop on HTML elements by setting the draggable attribute to false . We set draggable to false so we can't drag it. We add event listeners for the dragstart and drop events with addEventListener .

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.

What is the function of resize attribute?

The resize property defines if (and how) an element is resizable by the user. Note: The resize property does not apply to inline elements or to block elements where overflow="visible". So, make sure that overflow is set to "scroll", "auto", or "hidden".

How do you make a div draggable?

To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page.


3 Answers

In my specific case, here is what I did to fix it:

resizeableObj.resizable({
    start: function(event, ui) {        
        resizeableObj.css({
            position: "relative !important",
            top: "0 !important",
            left: "0 !important"
        });
    },
    stop: function(event, ui) {
        resizeableObj.css({
            position: "",
            top: "",
            left: ""
        });
    }
});

As you can see, in the resizable start event, set the position to relative (by using draggable and resizable in concert, an absolute positioning is added) and set the top and left to 0 to prevent the "jumping" that you sometimes see. In the stop event, reverse these changes. Works in my case, YMMV.

like image 87
naivedeveloper Avatar answered Oct 15 '22 11:10

naivedeveloper


Found a simpler solution (that fits my needs);

Inside jquery-ui.css, set the desired position and add !important to make sure it stays:

.ui-resizable {position: fixed !important} /* fix jquery's position absolute on resizing */
like image 34
Yonatan Ayalon Avatar answered Oct 15 '22 09:10

Yonatan Ayalon


Use this code to keep the element within the window space without scrolling while dragging:

$("#draggable").draggable({containment:"window", scroll: false})

like image 35
Ramsudharsan Manoharan Avatar answered Oct 15 '22 10:10

Ramsudharsan Manoharan