2016-12-18 214 views
0

我想在調整窗口大小時調整div的大小。用下面的代碼我得到「this.fullScreen不是一個函數」如果我刪除窗口調整大小它工作正常,但顯然不會調整窗口大小。我在想這個錯誤的方式嗎?當調整大小時,jquery窗口調整大小錯誤

var PE = {}; 

PE.functions = { 
    fullScreen: function fullScreen() { 
    var fullScreen = $('.full-screen'), 
     navbarHeight = $('.navbar').height(), 
     windowHeight = $(window).height(), 
     windowWidth = $(window).width(); 

    fullScreen.css({ 
     width: windowWidth, 
     height: windowHeight - navbarHeight 
    }); 
    }, 

    fullScreenResize: function screenResize() { 
    $(window).resize(function(){ 
     this.fullScreen(); 
    }); 
    } 
}; 

$(document).ready(function() { 
    PE.functions.fullScreenResize() 
}); 
+0

錯誤字面意思是「this.functionName不是函數」?如果沒有,請提供引用的實際函數名稱,以及指出您提供的代碼的哪一行位於回溯中​​的異常。 –

+0

對不起錯誤未捕獲TypeError:this.fullScreen不是函數 – Mark

回答

1

fullScreenResize,調用this.fullScreen()this不一定是PE.functions對象,因爲傳遞給resize回調函數有不同的this。爲了補救,bind回調函數,以目前的this

fullScreenResize: function screenResize() { 
    $(window).resize(function() { 
     this.fullScreen(); 
    }.bind(this)); 
} 

或者與完整的對象路徑PE.functions.fullScreen()更換this.fullScreen()

+0

完美謝謝,現在有道理。 – Mark