2013-01-17 70 views

回答

1

我最近一直在做類似的事情,並有一個很好的JavaScript函數,我使用:

var viewportwidth; 
var viewportheight; 

function resize() { 
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 

    if (typeof window.innerWidth != 'undefined') { 
     viewportwidth = window.innerWidth, 
     viewportheight = window.innerHeight 
    } 

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document) 

    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) { 
     viewportwidth = document.documentElement.clientWidth, 
     viewportheight = document.documentElement.clientHeight 
    } 

    // older versions of IE 

    else { 
     viewportwidth = document.getElementsByTagName('body')[0].clientWidth, 
     viewportheight = document.getElementsByTagName('body')[0].clientHeight 
    } 
} 

這將讓瀏覽器的當前高度和寬度。如果要檢查用戶何時調整頁面大小並調用resize()函數,只需使用簡單的JavaScript命令window.onresize=resize();

這是基本功能。從這裏開始,應該很容易對代碼進行一些更改。例如,如果你想要的網頁,只有當寬度大於或等於700被刷新,加上像這樣的resize()功能:

if(viewportwidth >= 700) { 
    window.reload(); 
} 
+0

我添加爲你工作的例子。看看這個鏈接:http://pastebin.com/ctyfbiWY在這個例子中,我用'alert(「reload」);'來表示頁面應該被重新加載。只需將其更改爲'window.reload();'即可實際重新加載頁面。 – Charles

+0

非常感謝你完美的作品! – user1741348

+0

隨意標記這是正確的答案,upvote我:) – Charles

相關問題