问题描述
我有一个div元素,使jQuery可调整大小.还设置了调整大小"选项,因此其他元素会同时调整大小.
我想做的是,以编程方式设置此Resizable div元素的大小,以触发所有Resizable逻辑(尤其是考虑了AlsoResize选项).
我该如何实现?
使用该方法向小部件添加方法,如下所示:
$.widget("ui.resizable", $.ui.resizable, {
resizeTo: function(newSize) {
var start = new $.Event("mousedown", { pageX: 0, pageY: 0 });
this._mouseStart(start);
this.axis = 'se';
var end = new $.Event("mouseup", {
pageX: newSize.width - this.originalSize.width,
pageY: newSize.height - this.originalSize.height
});
this._mouseDrag(end);
this._mouseStop(end);
}
});
这只是创建resizable
小部件正在寻找的鼠标事件并将其触发.如果您想做类似resizeBy
的事情,那将是一个更简单的结局,因为我们关心的只是增量:
var end = $.Event("mouseup", { pageX: newSize.width, pageY: newSize.height });
在jQuery UI之后和创建.resizable()
实例之前,您将调用$.widget()
方法,它们都将具有resizeTo
方法.那部分没有改变,只是:
$(".selector").resizable({ alsoResize: ".other-selector" });
然后调整大小,您将像这样调用新的resizeTo
方法:
$(".selector").resizable("resizeTo", { height: 100, width: 200 });
这就像您立即将其拖动到该大小一样.当然这里有一些陷阱:
-
"se"
轴假定您想按右下角调整大小-之所以选择它,是因为这是迄今为止最常见的情况,但是您可以将其设置为参数. - 我们会涉及一些内部事件,但是我故意使用尽可能少的内部实现细节,以免将来出现这种情况.
- 在将来的jQuery UI版本中,它可能会完全中断,我只是试图将这种可能性降到最低.
原始答案:
您可以执行以下操作:
$(".selector").trigger("resize");
alsoResize
在内部将处理程序绑定到resize
事件,因此您只需要调用它即可:)
I have a div element which is made jquery Resizable. It has alsoResize option set, so other elements resize simultaneously.
What I want to do, is to set size of this Resizable div element programmatically in such way, that all Resizable logic is triggered (especially this alsoResize option is taken into account).
How can I achieve that?
use that to add a method to the widget, like this:
$.widget("ui.resizable", $.ui.resizable, {
resizeTo: function(newSize) {
var start = new $.Event("mousedown", { pageX: 0, pageY: 0 });
this._mouseStart(start);
this.axis = 'se';
var end = new $.Event("mouseup", {
pageX: newSize.width - this.originalSize.width,
pageY: newSize.height - this.originalSize.height
});
this._mouseDrag(end);
this._mouseStop(end);
}
});
This is just creating the mouse events that the resizable
widget is looking for and firing those. If you wanted to do something like resizeBy
it'd be an even simpler end since all we care about is the delta:
var end = $.Event("mouseup", { pageX: newSize.width, pageY: newSize.height });
You'd call the $.widget()
method after jQuery UI and before creating your .resizable()
instances and they'll all have a resizeTo
method. That part doesn't change, it's just:
$(".selector").resizable({ alsoResize: ".other-selector" });
Then to resize, you'd call that new resizeTo
method like this:
$(".selector").resizable("resizeTo", { height: 100, width: 200 });
This would act as if you instantly dragged it to that size. There are of course a few gotchas here:
- The
"se"
axis is assuming you want resize by the bottom right - I picked this because it's by far the most common scenario, but you could just make it a parameter. - We're hooking into the internal events a bit, but I'm intentionally using as few internal implementation details as possible, so that this is less likely to break in the future.
- It could absolutely break in future versions of jQuery UI, I've only tried to minimize the chances of that.
You can play with it in action with a fiddle here and the resizeBy
version here.
Original answer:
You can do this:
$(".selector").trigger("resize");
alsoResize
internally rigs up a handler to the resize
event, so you just need to invoke that :)
这篇关于如何以编程方式触发jquery可调整大小的调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!