jQuery循环通过打开的对话框来获取/更新每个的位置

时间:2010-06-29 07:52:15

标签: jquery jquery-ui dialog positioning

我试图让jQuery对话框保持相对于窗口大小的位置,例如:

  • 如果原始视口宽度为900px且有2个对话框,一个在100,100,另一个在450,200
  • 当窗口调整为1200px时,第一个对话框为400,100,第二个对话框为750,200

我目前有这个代码处理窗口调整大小,但它没有更新对话框位置:

jQuery(window).resize(function() {
    widthCompensation = getBrowserWidths();
    jQuery(".dialog-class").each(function() {
        var myPosition = jQuery(this).dialog("option", "position");
        var newLeft = parseInt(myPosition.context.offsetLeft+widthCompensation);
        var newTop = parseInt(myPosition.context.offsetTop);
        jQuery(this).dialog("option", "position", [newLeft,newTop]);
    });
});

非常感谢任何帮助!

修改后的代码:

function getBrowserWidths() {
    var standardWidth = 990;
    var actualWidth = parseInt(jQuery(window).width());
    var ret = (actualWidth-standardWidth)/ 2;
    return(ret);
}
var leftPositions = new Array();
var widthCompensation = getBrowserWidths();

jQuery(window).resize(function() {
    widthCompensation = getBrowserWidths();
    jQuery(".target-class").each(function(i) {
        var newLeft = leftPositions[i]+widthCompensation; 
        console.log(newLeft);
        jQuery(this).css({"left": newLeft+"px"});
    });
});

//loop through draggables to get their initial positions
jQuery(".target-class").each(function(i) {
    var myPosition = jQuery(this).offset();
    leftPositions[i] = myPosition.left;
});

1 个答案:

答案 0 :(得分:0)

在每个中使用它,它对我来说非常有效(如果对话框没有到达浏览器的角落。

var myPosition = $(this).offset();
var newLeft = myPosition.left+widthCompensation; 
var newTop = myPosition.top;    
$(this).dialog("option", "position", [newLeft,newTop]);

希望它有所帮助;)

对于每个,您可以使用索引(此处为i),

var newLeft = new Array();
var newTop = new Array();
$(".dialog-class").each(function(i) {
  var myPosition = $(this).offset();
  var newLeft[i] = myPosition.left+widthCompensation; 
  var newTop[i] = myPosition.top;   
  $(this).dialog("option", "position", [newLeft[i],newTop[i]]);
});

没有测试它,所以不是100%肯定,但这是想法。