JQueryUI - draggable element gets dropped onto multiple droppable areas if the droppable areas are scaled down

前端 未结 2 521
悲&欢浪女
悲&欢浪女 2021-01-03 12:17

I am trying to drag and drop and element onto a droppable area. Let\'s say that I have multiple droppable areas with the same class and I have written a d

相关标签:
2条回答
  • 2021-01-03 12:29

    Found a workaround , posting it here just in case it helps anyone else.

    I had to modify jquery-ui.js.

    m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight};

    to

    m[i].proportions = { width: m[i].element[0].offsetWidth*scaleFactor, height: m[i].element[0].offsetHeight*scaleFactor };
    

    where scaleFactor is initialized to 1 and is changed in your javascript code to the value of css-transform , i.e., If you use -webkit-transform:scale(0.3,0.3) , set the scaleFactor to 0.3 and bingo , you are done!

    Updated fiddle using the changed jquery-ui.js file

    http://jsfiddle.net/P4FMr/4/

    0 讨论(0)
  • 2021-01-03 12:32

    using the response from Harsha Venkatram and the comments in here I was finally able to fix this in an application I am working on. The application is based on WordPress and it uses a minified droppable.min.js included by Wordpress under wp-includes.

    The application is an editor which has a zoom so its scalable and theres some drag and drop action of images in it. But when dragging images into certain zones it triggered multiple zones around it when being scaled down which messed up your work! And the application was built in a way that its always somewhat scaled down so it did not work well...

    ..So in the end I was able to apply the fix directly in the minified droppable code.

    First in the scaling function, there was an x number representing the scaling so I initialized window.currentZoom in my javascript code and made the scaling function update the value of window.currentZoom to the scaling number everytime it runs, so my variable window.currentZoom always contains my scaling value, and since its a window. variable I know I can access it from anywhere.

    So in wp-includes/js/jquery/ui/droppable.min.js I changed these:

    from

    this.proportions=function(){if(!arguments.length)return e||(e={width:this.element[0].offsetWidth,height:this.element[0].offsetHeight});
    

    to

    this.proportions=function(){if(!arguments.length)return e||(e={width:this.element[0].offsetWidth*window.currentZoom,height:this.element[0].offsetHeight*window.currentZoom});
    

    from

    o[i].proportions({width:o[i].element[0].offsetWidth,height:o[i].element[0].offsetHeight}))}}
    

    to

    o[i].proportions({width:o[i].element[0].offsetWidth*window.currentZoom,height:o[i].element[0].offsetHeight*window.currentZoom}))}}
    

    And it fixed the bug with scaling and droppable omg!

    I just thought maybe it could help someone else. Good luck!

    Oh and probably I should copy this file in my theme, make sure WP update doesnt erase my change.

    In the end copying this file in my theme never worked so instead I downloaded a fresh jquery-ui from jquery and put it in my theme. In there I found these lines which I modified. That worked too..

    // Retrieve or derive the droppable's proportions
                return proportions ?
                    proportions :
                    proportions = {
                        width: this.element[ 0 ].offsetWidth*window.currentZoom,
                        height: this.element[ 0 ].offsetHeight*window.currentZoom
                    };
    
    
    // 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*window.currentZoom,
                height: m[ i ].element[ 0 ].offsetHeight*window.currentZoom
            } );
    
    0 讨论(0)
提交回复
热议问题