Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery droppable area wrong if zoomed

Here is the fiddle with comment: http://jsfiddle.net/7xw1m1qd/1/

Html for example:

<div class="droppable"></div>
<div class="drag"></div>

JS for example:

$('.drag').draggable({});

$('.droppable').droppable({
    out: function() {
      $('.drag').css('background-color', 'red');
    },

    drop: function() {
      $('.drag').css('background-color', 'green');
    },
});

CSS for example:

.droppable {
    width: 200px;
    height: 200px;
    transform: scale(2);    
    -webkit-transform: scale(2);    
    -ms-transform: scale(2); 
    background-color: #C3C3C3;
}

.drag {
    background-color: black;
    width: 20px;
    height: 20px;
    z-index: 10;
    position: absolute;
    top: 10px;
    left: 350px;    
}

The problem is: If droppable is zoomed (by transform: scale), it still uses original dimensions for droppable, so i can drop element only in original bounds of droppable.

I found some similiar questions, but not found working solution.

like image 497
JakubK Avatar asked Feb 14 '26 14:02

JakubK


1 Answers

Thanks Ferret. I solved my problem using code from Your link

Just added this code before my draggable/droppable code:

$.ui.ddmanager.prepareOffsets = function( t, event ) {
    var i, j,
        m = $.ui.ddmanager.droppables[ t.options.scope ] || [],
        type = event ? event.type : null, // workaround for #2317
        list = ( t.currentItem || t.element ).find( ":data(ui-droppable)" ).addBack();

    droppablesLoop: for ( i = 0; i < m.length; i++ ) {

        // No disabled and non-accepted
        if ( m[ i ].options.disabled || ( t && !m[ i ].accept.call( m[ i ].element[ 0 ], ( t.currentItem || t.element ) ) ) ) {
            continue;
        }

        // Filter out elements in the current dragged item
        for ( j = 0; j < list.length; j++ ) {
            if ( list[ j ] === m[ i ].element[ 0 ] ) {
                m[ i ].proportions().height = 0;
                continue droppablesLoop;
            }
        }

        m[ i ].visible = m[ i ].element.css( "display" ) !== "none";
        if ( !m[ i ].visible ) {
            continue;
        }

        // Activate the droppable if used directly from draggables
        if ( type === "mousedown" ) {
            m[ i ]._activate.call( m[ i ], event );
        }

        m[ i ].offset = m[ i ].element.offset();
        m[ i ].proportions({ width: m[ i ].element[ 0 ].offsetWidth * MyEditor.currentZoom, height: m[ i ].element[ 0 ].offsetHeight * MyEditor.currentZoom });
    }

};

This is performed after jquery and jquery-ui load, and it helps. Thanks a lot.

like image 72
JakubK Avatar answered Feb 17 '26 02:02

JakubK