How can I make a jQuery functions execute on load

2019-08-27 22:42发布

问题:

How can I make this function run on load and on window resize? thanks.

jQuery(function( $ ) {
var headerHeight = $("#header").outerHeight();
var viewportHeight = $(window).height();
var elementNewHeight = viewportHeight - headerHeight;
$(".fullHeightThis").outerHeight(elementNewHeight);
});

回答1:

jQuery(function( $ ) {
    function someStuff(){
      var headerHeight = $("#header").outerHeight();
      var viewportHeight = $(window).height();
      var elementNewHeight = viewportHeight - headerHeight;
      $(".fullHeightThis").outerHeight(elementNewHeight);
    }
    someStuff(); //call immediately

    $(window).on('resize', someStuff);

});