我遇到了类似的问题,但使用鼠标位置和当前缩放提出了这个解决方案。在此示例中,我假设您有一个名为的变量current_zoom
,它将当前缩放作为浮点数,还有一个名为的变量maintainAspectRatio
,表示元素是否要保持其纵横比。
此代码仅支持使用南和东手柄调整大小。如果要使用任何其他代码,则需要扩展代码。例如,我还实现了东南手柄的代码。但是,为了展示最低限度,我将其省略了:
$('.box').resizable({
...
resize: function(event, ui) {
var $element = ui.element;
// Gets the axis that the user is dragging.
var axis = $element.data('ui-resizable').axis;
// Get the dimensions of the element's bounding box. We can't use
// leftOffset + $element.width() because it doesn't factor in the
// current zoom. Morever, we can't just multiply $element.width()
// by the current zoom to get the zoomed width because it still
// gives the wrong value. So we have to be sneaky and use resizable's
// resizer overlays to get the right and bottom values.
var leftOffset = $element.offset().left;
var topOffset = $element.offset().top;
var rightOffset = $element.children('.ui-resizable-e').offset().left;
var bottomOffset = $element.children('.ui-resizable-s').offset().top;
// We are using the mouse location to calculate the width (during
// a horizontal resize) and the height (during a vertical resize)
// of the element. This is because we need the resize box to keep
// up with the mouse when the user is not at 100% zoom.
var mouseX = event.pageX;
var mouseY = event.pageY;
// Must remove the zoom factor before setting width and height.
var mouseCalculatedWidth = (mouseX - leftOffset) / current_zoom;
var mouseCalculatedHeight = (mouseY - topOffset) / current_zoom;
var offsetWidth = (rightOffset - leftOffset) / current_zoom;
var offsetHeight = (bottomOffset - topOffset) / current_zoom;
// In order to maintain aspect ratio, we manually calculate it.
var aspectRatio = offsetHeight / offsetWidth;
// Resizing using the east handler, set the width of the element.
// If this element maintains its aspect ratio, also set the height.
if (axis == "e") {
$element.width(mouseCalculatedWidth);
if (maintainAspectRatio) {
$element.height(mouseCalculatedWidth * aspectRatio);
}
}
// Resizing using the south handler, set the height of the element.
// If this element maintains its aspect ratio, also set the width.
if (axis == "s") {
$element.height(mouseCalculatedHeight);
if (maintainAspectRatio) {
$element.width(mouseCalculatedHeight / aspectRatio);
}
}
}
...
});// Resizable