是否可以添加遏制(限制在另一个元素的边界)jQuery UI的Dialog?如何设置jQuery UI对话框上的遏制?
5
A
回答
3
您可以定位对话框并对其应用遏制。试试这个:
var container = $('.dialog-container'),
dialog = $('.ui-dialog');
// get container top left corner locations
var cx1 = container.offset().left,
cy1 = container.offset().top;
// get dialog size
var dw = dialog.outerWidth(),
dh = dialog.outerHeight();
// get container bottom right location, then subtract the dialog size
var cx2 = container.width() + cx1 - dw,
cy2 = container.height() + cy1 - dh;
dialog.draggable("option", "containment", [cx1, cy1, cx2, cy2]);
编辑:我为您设置了a demo。
EDIT2:改到正确的轨道上使用对话outerWidth & outerHeight
9
@ Mottie的,但有一个简单,更好的解决方案:
var container = $('.dialog-container'),
dialog = $('.ui-dialog');
dialog.draggable("option", "containment", container);
不像Mottie的解决方案,这将不若视口调整大小打破。我已经分叉JSFiddle here。
0
@Mac在正确的轨道上,但解决方案不完整。您必须必须也设置对话框的Resizable小部件的包含。如果你只设置了Draggable,当你拖动时你会得到控制,但当你抓住边缘并调整大小时,你仍然会出界。
所以你要这样做,假设#mydialog
是你最初创建对话框的元素,而#boundary
是你想限制它的元素(顺便说一下,容器参数也可以是选择器) :
let ui = $('#mydialog').closest('.ui-dialog'); // get parent frame
ui.draggable('option', 'containment', '#boundary'); // <-- drag, but not resize
ui.resizable('option', 'containment', '#boundary'); // <-- don't forget!!!
下面是一个例子片断,切换复选框切换'document'
(默认值),并且'#area'
之间的对应小部件的限制。然后尝试通过标题栏和拖动对话框来调整其边缘的大小。注意当您只选择 “限制拖”,会发生什么:
// Create dialog from #win with mostly default options.
$('#win').dialog({
width: 200,
height: 150,
position: { my: 'center', at: 'center', of: '#area' }
});
// When checkbox changed, update confinement settings.
$('#draggable, #resizable').change(function() {
let d = $('#draggable').prop('checked');
let r = $('#resizable').prop('checked');
let ui = $('#win').closest('.ui-dialog');
ui.draggable('option', 'containment', d ? '#area' : 'document');
ui.resizable('option', 'containment', r ? '#area' : 'document');
});
#area {
position: relative;
left: 2ex;
top: 2ex;
width: 400px;
height: 300px;
border: 1px solid red;
}
#win {
font-size: 10pt;
display: flex;
flex-direction: column;
}
label {
display: flex;
align-items: center;
}
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<div>Example</div>
<div id="area"></div>
<div id="win" title="test">
<label><input type="checkbox" id="draggable">Contain drag</label>
<label><input type="checkbox" id="resizable">Contain resize</label>
</div>
</body>
相关问题
- 1. 设置jQuery UI对话框按钮ID?
- 2. 定制jQuery UI对话框
- 3. jQuery UI对话框 - 位置
- 4. 如何设置jquery UI对话框的默认值
- 5. 如何在JQuery UI对话框中设置元素的值
- 6. jquery ui遏制与滚动
- 7. 在jQuery UI对话框上设置圆角类型
- 8. jQuery UI设置绝对对话框位置什么都不做
- 9. Jquery UI对话框
- 10. Jquery UI对话框
- 11. JQUERY UI对话框
- 12. jQuery UI对话框动态设置输入框的文本
- 13. 位置jQuery UI的对话框
- 14. jQuery UI的对话框 - 设置默认配置
- 15. 在jQuery UI的对话框
- 16. jquery ui对话框,有多个对话框影响位置
- 17. 刷新jquery ui对话框位置
- 18. 如何在Jquery UI对话框中实现“确认”对话框?
- 19. 如何从对话框代码中关闭jQuery UI对话框?
- 20. JQuery UI-对话框:如何在对话框中进行回调?
- 21. 如何定位jQuery UI对话框
- 22. 如何使用jQuery UI对话框
- 23. 设置jquery ui的对话框模式宽度?
- 24. jQuery UI:设置对话框的拖动选项
- 25. jQuery UI对话框自动设置iframe的宽度
- 26. 如何在Jetpack中复制jQuery UI的对话框功能?
- 27. 如何覆盖jQuery-UI对话框在标记中的位置?
- 28. jQuery UI对话框 - 定位
- 29. IE7,JQuery UI对话框
- 30. jQModal或jQuery UI对话框?
通过你的意思是遏制....?我不清楚你在这里后的情况,澄清一下? – 2010-05-25 10:08:15
嗯,我的意思是我想指定对话框可以拖动的区域。现在它可以拖动到浏览器窗口的各个地方.. – PPPHP 2010-05-25 10:26:02