干燥窗口调整大小功能的最佳方法

时间:2017-12-12 08:20:30

标签: jquery function dry

这看起来不干嘛我怎么能更有效地重构这个?

var headerHeight = $('header').outerHeight();
var sliderHeight = $('.slick-slider').outerHeight();
$('.slider-container').css('height', sliderHeight - headerHeight);

$(window).resize(function() {
  clearTimeout(window.resizedComplete);
  window.resizedComplete = setTimeout(function() {
    var headerHeight = $('header').outerHeight();
    var sliderHeight = $('.slick-slider').outerHeight();
    $('.slider-container').css('height', sliderHeight -
      headerHeight);
  }, 250);
});

1 个答案:

答案 0 :(得分:2)

通常的方法:制作并使用可重复使用的功能:

function doHeightStuff() {
    var headerHeight = $('header').outerHeight();
    var sliderHeight = $('.slick-slider').outerHeight();
    $('.slider-container').css('height', sliderHeight - headerHeight);
}
doHeightStuff();
$(window).resize(function() {
  clearTimeout(window.resizedComplete);
  window.resizedComplete = setTimeout(doHeightStuff, 250);
});