这看起来不干嘛我怎么能更有效地重构这个?
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);
});
答案 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);
});