2012-08-30 46 views
2

我打开一个窗口w=window.open('','', 'width=1000,height=700');比我想这个窗口的比例10/7调整大小。例如: :500/350; 100/70 ...JavaScript窗口调整比例

我最大和最小尺寸已经:

var w; 
function openwindow() 
{ 
    w=window.open('','', 'width=1000,height=700'); 
    w.focus(); 

    resize(w); 
} 

function resize(w_obj) { 


     w_obj.onresize = function(event) { 
      width = w_obj.innerWidth; 
      height = w_obj.innerHeight; 

      // if popup width is greather then 1000px 
      if (width > 1000) { 
       w_obj.resizeTo(1000,700); 
      } 

      // if popup height is greather then 700px 
      if(height >700) { 
       w_obj.resizeTo(1000,700); 
      } 


      if (width < 500) { 
       w_obj.resizeTo(500,350); 
      } 


      if(height < 350) { 
       w_obj.resizeTo(500,350); 
      } 

     } 


} 
+0

'w_obj.resizeTo(width,width * 7/10);'? – David

+0

@David我已经在w_obj.onresize函数中插入了你的代码,并且它引起了无限循环窗口调整大小:( –

回答

1

您应该使用outerHeight因为这就是resizeTo的参数的含义。然而,问题在于您无法知道用户是调整窗口的宽度还是高度。因此,您不知道使用哪个维度作为参考以及要计算哪个维度。

总之,你想要的是以下几点:

  • width,最小和最大尺寸之间height。您可以使用Math.min/Math.max
  • 设置高度为width乘以7/10比率。

请参阅http://jsfiddle.net/bSE9C/1/

var width = w_obj.outerWidth; 
var height = w_obj.outerHeight; 

width = Math.min(width, 1000); // may not exceed 1000 maximum 
height = Math.min(height, 700); 

width = Math.max(width, 500); // may not exceed 500 minimum 
height = Math.max(height, 350); 

height = width * 7/10; 

w_obj.resizeTo(width, height); 
+0

它的工作原理,谢谢:) –

+0

但不适用于Opera和IE –

+0

onresize事件无法在歌剧上运行 –