2015-04-22 28 views
1
http://stackoverflow.com/questions/29788754/open-multiple-child-windows-with-one-click 

^^我剛剛收到一些幫助,把一個小網站放在一起,現在我對它有不同的想法。讓孩子的窗戶出現在隨機位置

http://alexalmaguer.site90.com/

每當我點擊一個按鈕,最多5個窗口出現,他們都出現在屏幕中央,因爲劇本的下面,告訴他們這樣做。當只有一個窗口打開時,這很好,但現在它們都顯示在彼此之上。有什麼辦法可以改變這個窗口使窗口出現在隨機位置?

function wopen(url, name, w, h) 
{ 

    w += 32; 
    h += 96; 
    wleft = (screen.width - w)/2; 
    wtop = (screen.height - h)/2; 

    if (wleft < 0) { 
    w = screen.width; 
    wleft = 0; 
    } 
    if (wtop < 0) { 
    h = screen.height; 
    wtop = 0; 
    } 
    var win = window.open(url, name, 
    'width=' + w + ', height=' + h + ', ' + 
    'left=' + wleft + ', top=' + wtop + ', ' + 
    'location=no, menubar=no, ' + 
    'status=no, toolbar=no, scrollbars=no, resizable=no'); 

    win.resizeTo(w, h); 

    win.moveTo(wleft, wtop); 
    win.focus(); 
} 
+0

是的。隨機wtop和wleft。我很好奇你爲什麼在一個位置打開窗戶,然後也將它移動到相同的位置 – mplungjan

+0

嗨亞歷山大,你是否嘗試給wopen函數調用中的w,h賦予不同的值? – Phoenix

+0

wtop and wleft,not w and h – mplungjan

回答

1
    隨機化
  1. WLEFT和/或WTOP
  2. 刪除空格在參數和任何=無參數,因爲這是缺省值。
  3. ,除非他們需要在古老的瀏覽器

function wopen(url, name, w, h) { 

    w += 32; 
    h += 96; 
    wleft = (screen.width - w)/2; 
    wtop = (screen.height - h)/2; 

    if (wleft < 0) { 
    w = screen.width; 
    wleft = 0; 
    } 
    if (wtop < 0) { 
    h = screen.height; 
    wtop = 0; 
    } 
    if (wleft > 0) { 
    wleft = Math.floor(Math.random() * wleft); // for example 
    } 
    var win = window.open(url, name, 
    'width=' + w + ',height=' + ',left=' + wleft + ',top=' + wtop); 

    win.focus(); 
} 

選擇是不是隨機化,但要記住的座標,每次

另一種方法是增加窗口寬度刪除不必要的功能打開窗口左上角對齊鼠標點擊座標

+1

這個效果很好!謝謝! –