2016-03-01 84 views
0

我有這方面的工作的JavaScript代碼確實對initresize一些調整大小:的Javascript:代碼投入功能時,沒有得到執行

(function (w) { 

    w.init = function() { 

    var w = window.innerWidth; 
    var h = window.innerHeight; 

    //resize_font; 
    var fs = parseInt(w/26); 
    document.body.style.fontSize = fs + "px"; 

    //resize_cover; 
    var logo = get_by_id("logo"); 
    var logo_height = logo.clientHeight; 
    var menu = get_by_id("menu"); 
    var menu_height = menu.clientHeight; 
    var cover_height = logo_height + menu_height; 
    var distance_top = (h - cover_height)/2.5; 

    var container = get_by_id("container"); 
    container.style.paddingTop = distance_top + "px"; 
    } 

    w.resize = function() { 

    var w = window.innerWidth; 
    var h = window.innerHeight; 

    //resize_font; 
    var fs = parseInt(w/26); 
    document.body.style.fontSize = fs + "px"; 

    //resize_cover; 
    var logo = get_by_id("logo"); 
    var logo_height = logo.clientHeight; 
    var menu = get_by_id("menu"); 
    var menu_height = menu.clientHeight; 
    var cover_height = logo_height + menu_height; 
    var distance_top = (h - cover_height)/2.5; 

    var container = get_by_id("container"); 
    container.style.paddingTop = distance_top + "px"; 
    } 
} 

現在我想重構代碼,用於調整字體和翻唱成一個函數,但 我無法讓它工作。我試過這個:

(function (w) { 

    w.init = function() { 
    var w = window.innerWidth; 
    var h = window.innerHeight; 

    resize_font; 
    resize_cover; 
    } 

    w.resize = function() { 
    var w = window.innerWidth; 
    var h = window.innerHeight; 

    resize_font; 
    resize_cover; 
    } 

    function resize_cover() { 
    var logo = get_by_id("logo"); 
    var logo_height = logo.clientHeight; 
    var menu = get_by_id("menu"); 
    var menu_height = menu.clientHeight; 
    var cover_height = logo_height + menu_height; 
    var distance_top = (h - cover_height)/2.5; 

    var container = get_by_id("container"); 
    container.style.paddingTop = distance_top + "px";   
    } 

    function resize_font() { 
    var fs = parseInt(w/26); 
    document.body.style.fontSize = fs + "px";   
    } 
} 

控制檯不顯示任何錯誤,但調整大小沒有發生。我究竟做錯了什麼?

+1

Ehhh,究竟你認爲'resize_font;'呢? – Amit

+0

呃,調整font_size的大小。 – Flip

回答

3

你需要在init和resize事件函數中調用你的函數。 您有:

resize_font; 
resize_cover; 

您需要:

resize_font(); 
resize_cover(); 

一個函數的名稱就是對它的引用,這就是爲什麼你沒有看到任何錯誤。你現在有類似說法:

var x = 1; 
x; 
+1

freakin'JS syntax ... thx! – Flip

2

看起來你的問題是這樣的:

resize_font; 
resize_cover; 

我想你的意思

resize_font(); 
resize_cover(); 
1

這好像你忘了執行功能,剛纔提到了指針。

請嘗試更改:

resize_font; 
resize_cover; 

resize_font(); 
resize_cover();