干涸 window 调整大小功能的最佳方式
Best way to DRY up window resize function
这看起来并不干,我怎样才能更有效地重构它?
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);
});
通常的方法:制作并使用可重用函数:
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);
});
这看起来并不干,我怎样才能更有效地重构它?
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);
});
通常的方法:制作并使用可重用函数:
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);
});