當我向下滾動到我的網站時,我想讓我的標題降低其高度。所以我寫了這個代碼:調整窗口大小Jquery
function resizeHeader() {
if ($(window).width() > 1200) {
if ($('body').hasClass('scrolled')) {
$('#masthead .top-header').css('height', '50px');
} else {
//Initial height
$('#masthead .top-header').css('height', 'auto');
}
} else if ($(window).width() > 768 && $(window).width() < 989) {
if ($('body').hasClass('scrolled')) {
$('#masthead .top-header').css('height', '35px');
} else {
$('#masthead .top-header').css('height', 'auto');
}
}
}
/** Fix header scroll **/
$(window).scroll(function() {
if ($(this).scrollTop() > 1) {
$('body').addClass('scrolled');
resizeHeader();
} else {
$('body').removeClass('scrolled');
resizeHeader();
}
});
所以此代碼的工作,當我刷新我的網頁在想要的大小,腳本工作。
然而,當我刷新,然後我調整我的窗口,那麼我的頭有沒有我這個大小設置高度
我想,當我調整我的窗口,該功能正在運行,並做動作。它在刷新我的頁面時總是在工作,而不是在調整它的大小時。
感謝您的幫助!
正如Rory所建議的那樣,您最好使用[CSS媒體查詢](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries) –