views:

36

answers:

1

I am trying to make the jQuery dialogs stay in their position relative to the window size, so for instance: if the original viewport width is 900px and there are 2 dialogs, one at 100,100 and another at 450,200 when the window is resized to say 1200px the first dialog would be 400,100 and the second at 750,200

I currently have this handling the window resize, but it doesn't update the dialog positions:

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]);
    });
});

Any help would be greatly appreciated!

Modified code:

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;
});
A: 

Using this in the each, it quite worked for me (if the dialog doesn't reach the corner of the browser.

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

Hope it helps ;)

For the each, you can use the Index (here 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]]);
});

Didn't test it so not 100% sure, but here is the idea.

Michael Lumbroso
thanks, using that as a basis I have managed to get the positions to update on resize although I need to store their original position somewhere as this ends up with exponential incrementation of the left and top positions (as it's always adding the offset to it's current position, need to add the offset to the individual dialog start positions). Is there any way to assign a variable to each instance?
Dan
There is a crappy way I'm thinking of, don't know if there is neater one, I guess so, but I'm not a specialist. I'm editing my answer ;)
Michael Lumbroso
Was the solution ok?
Michael Lumbroso
Sorry I got some urgent jobs to do so didn't have chance to take a look. I think this is almost what I'm looking for, but wouldn't it still cause the same exponential issue as it's updating the array every time? I need to be able to handle adding extra dialogs into the page and I can't guarantee that if I add to the array when a new dialog is created that jQuery will loop through them in the correct order. Each dialog has a unique ID that I could reference if that helps? Or could I assign the variable to each individual object?
Dan
If that's your question on a simple selector like a class, the order of the elements in the each will match the order in which they appear in the DOM. The order matters for the process? Because here, it calculates the position of each element at each resize, maybe this is not really optimized, but I don't see how to improve this. Btw, the initialization of arrays, would better be performed outside the resize function to avoid useless assignments.
Michael Lumbroso
Dan