调整大小时jquery窗口调整大小错误

时间:2016-12-18 14:16:11

标签: javascript jquery resize

我正在尝试在调整窗口大小时调整div的大小。使用下面的代码我得到“this.fullScreen不是一个函数”如果我删除窗口调整大小它工作正常,但显然不会调整窗口大小。我是否以错误的方式思考这个问题?

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()
});

1 个答案:

答案 0 :(得分:1)

fullScreenResize中,对this.fullScreen()this的调用不一定是PE.functions对象,因为传递给resize的回调函数具有不同的this 1}}。要解决bind回调函数到当前this

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

或使用完整对象路径this.fullScreen()替换PE.functions.fullScreen()