


How to Achieve Correct jQuery Drag/Resize Behavior with CSS Transform Scale?
Fixing jQuery Drag/Resize with CSS Transform Scale
Background
Applying CSS transforms to an element and then using jQuery's draggable and resizable plugins on its children can introduce misalignment between the mouse cursor and the dragged or resized element. This is due to the scale factor applied to the element.
jQuery Patching Solution
A previous solution involved patching jQuery's draggable and resizable plugins to adjust the mouse offset by the scale factor. This method required modifying the source code of the plugins.
Alternative Solution: Callback Handlers
An alternative solution is to use callback handlers provided by the resizable and draggable plugins. This eliminates the need for patching jQuery.
Resizable Fix
<br>$(this).resizable({</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">minWidth: -(contentElem.width()) * 10, minHeight: -(contentElem.height()) * 10, resize: function(event, ui) { var changeWidth = ui.size.width - ui.originalSize.width; var newWidth = ui.originalSize.width + changeWidth / zoomScale; var changeHeight = ui.size.height - ui.originalSize.height; var newHeight = ui.originalSize.height + changeHeight / zoomScale; ui.size.width = newWidth; ui.size.height = newHeight; }
});
In the resize handler, calculate the change in width and height, apply the inverse of the zoom scale to adjust the new width and height, and update the ui.size object accordingly.
Draggable Fix
<br>$(this).draggable({</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">handle: '.drag-handle', start: function(event, ui) { ui.position.left = 0; ui.position.top = 0; }, drag: function(event, ui) { var changeLeft = ui.position.left - ui.originalPosition.left; var newLeft = ui.originalPosition.left + changeLeft / (( zoomScale)); var changeTop = ui.position.top - ui.originalPosition.top; var newTop = ui.originalPosition.top + changeTop / zoomScale; ui.position.left = newLeft; ui.position.top = newTop; }
});
In the drag handler, calculate the change in left and top positions, apply the inverse of the zoom scale to adjust the new left and top positions, and update the ui.position object accordingly.
Conclusion
This alternative solution using callback handlers provides a non-invasive way to correct the drag/resize behavior when CSS transforms with scale are applied.
The above is the detailed content of How to Achieve Correct jQuery Drag/Resize Behavior with CSS Transform Scale?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
