我试图让jQuery对话框保持相对于窗口大小的位置,例如:
我目前有这个代码处理窗口调整大小,但它没有更新对话框位置:
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;
});
答案 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%肯定,但这是想法。