在滚动和最大宽度上启动脚本

Start script on scroll and max width

本文关键字:启动 脚本 滚动      更新时间:2023-09-26

我知道了:

JavaScript:

$(window).scroll(function(){
if  ($(window).scrollTop() >= 100){
     $('#normal_menu').css({height: '50px'});
} else {
     $('#normal_menu').css({height: '120px'});
    }
});  

工作得很好。但是现在我希望代码只在屏幕宽度小于924px时工作。我发现这:if( $(window).width() < 924),但我如何在我的代码中实现这一点?

试试这个:

$(window).scroll(function () {
    if($(window).width() < 924) { // <--- inserted if statement here.
    if ($(window).scrollTop() >= 100) {
        $('#normal_menu').css({
            height: '50px'
        });
    } else {
        $('#normal_menu').css({
            height: '120px'
        });
    }
    } // <--- inserted closing bracket here.
});

p。S:这将只工作,如果窗口的width小于924 ....所以即使它是924,它也不会工作!