2017-06-06 104 views
1

我想设置jQuery UI(1.12.1)对话框的绝对位置(x和y)。从各方面来看,我应该能够做到这一点,像这样(虽然jQuery的文档莫名其妙地不提这句法,既不here也不here):人成功地使用这种语法jQuery UI设置绝对对话框位置什么都不做

$('#element').dialog('option', 'position', [x, y]); 

例子是:

然而,当我这样做时,没有任何反应。请看下面的例子(可能要运行全屏幕):

$('#test').dialog({ 
 
    width: 200, 
 
    height: 200 
 
}); 
 

 
$('#move').click(function() { 
 
    $('#test').dialog('option', 'position', [30, 30]); 
 
});
<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> 
 
<body> 
 
<button id="move">Move</button> 
 
<div id="test" title="test">This is a test.</div> 
 
</body>

运行它,按下按钮,对话框不动。

为什么这不适合我,我该如何设置对话框的x,y位置?

请注意,对话框在位置更改后必须保持可拖动和可调整大小。

回答

1

我想我得到它的工作..用

position: { my: "left top", at: "left+"+x+" top+"+y+"", of: window } 

,并通过读取.position() documentation

我达到这个

$('#test').dialog({ 
 
    width: 200, 
 
    height: 200 
 
}); 
 

 
$('#move').click(function() { 
 
    var x = 30, 
 
     y = 30; 
 
    $('#test').dialog({ 
 
    //position: { my: "left top", at: "left bottom", of: window } 
 
    position: { my: "left top", at: "left+" + x + " top+" + y + "", of: window } 
 
    }); 
 
});
<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> 
 
<body> 
 
<button id="move">Move</button> 
 
<div id="test" title="test">This is a test.</div> 
 
</body>

+0

真棒。是的,它运作良好。谢谢!我要这样做,但我仍然有点好奇为什么'[x,y]'不适合我。谁知道... –

+0

@JasonC当对话框文件是[读取对话框位置](https://api.jqueryui.com/dialog/#option)时,我不知道你在哪里阅读了关于'[x,y] -位置) –