如何使用 jQuery UI 可调整大小仅水平或垂直调整大小?
我找到的唯一解决方案是使用当前值设置最大和最小高度或宽度。
示例:
foo.resizable({
maxHeight: foo.height(),
minHeight: foo.height()
});
但这确实很难看,特别是如果我必须以编程方式更改元素的高度。
The only solution I've found is to set the max and min height or width with the current value.
Example:
foo.resizable({
maxHeight: foo.height(),
minHeight: foo.height()
});
But this is really ugly, especially if I have to change the element's height programmatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以将调整大小
handles
选项设置为仅显示在左侧和右侧(或东/西),如下所示:您可以在这里尝试一下。
You could set the resize
handles
option to only show on the left and right (or east/west), like this:You can give it a try here.
虽然海报主要要求仅水平调整大小,但该问题也要求垂直调整。
垂直情况有一个特殊问题:您可能设置了一个相对宽度(例如 50%),即使在调整浏览器窗口大小时也希望保留该宽度。然而,一旦您第一次调整元素大小,jQuery UI 就会设置一个以 px 为单位的绝对宽度,并且相对宽度会丢失。
如果发现以下代码适用于此用例:
另请参阅 http://jsfiddle.net/cburgmer/wExtX / 和 jQuery UI 可调整大小:自动高度单独使用东手柄时
While the poster was mainly asking for a horizontal-only resize, the question does ask for vertical, too.
The vertical case has a special issue: You might have set a relative width (e.g. 50%) that you want to keep even when the browser window is resized. However jQuery UI sets an absolute width in px once you first resize the element and the relative width is lost.
If found the following code to work for this use case:
See also http://jsfiddle.net/cburgmer/wExtX/ and jQuery UI resizable : auto height when using east handle alone
一个非常简单的解决方案:
这也适用于draggable()。
示例: http://jsfiddle.net/xCepm/
A very simple solution:
This works for draggable() as well.
Example: http://jsfiddle.net/xCepm/
解决方案是配置您的可调整大小,以便取消您不需要的尺寸的更改。
这是仅垂直调整大小的示例:
The solution is to configure your resizable so it cancels changes of the dimension you don't want.
here is an example of vertically only resizable:
我想允许在浏览器调整大小时调整宽度,但在用户更改元素大小时不允许调整宽度,这工作得很好:
I wanted to allow re-size the width when browser re-sizes, but not when user changes the size of element, this worked fine:
对于我来说,同时具有句柄和调整大小处理程序的解决方案工作得很好,因此用户可以看到句柄,但只能水平调整大小,类似的解决方案应该适用于垂直方向。如果没有右下处理程序,用户可能不知道他可以调整元素的大小。
当使用
ui.size.height = ui.originalSize.height;
时,如果元素从初始状态更改了其大小,它将无法正常工作。为了获得更好的性能,可以删除
$(this)
:For me solutions with both handles and
resize
handler worked fine, so user see handle but can resize only horizontally, similar solution should work for vertical. Without bottom right handler user might be unaware that he can resize element.When using
ui.size.height = ui.originalSize.height;
it will not work properly if element changed it's size from initial state.For better performance
$(this)
could be removed:成功地在垂直和水平方向上工作 div 位置
Successfully working div position Vertically and horizontally