jquery if语句基于屏幕大小

Drh*_*des 12 jquery toggle screen-size

我有一个滑出导航栏,我想在屏幕宽度> = 1024时默认打开,默认情况下关闭<1024.我有一个按钮,可以打开并关闭它.我刚刚开始学习js.我想如果窗口宽度> = 1024,有一种方法可以在if语句中设置默认切换状态.任何帮助将不胜感激.这是我到目前为止切换的内容.

$('a.expand').toggle(function() {
        $(this).addClass("open");
        $('#nav').animate({width: 50},{queue:false, duration:300});
        $('.wrapify').animate({marginLeft: 50},{queue:false, duration:300});
        $('.primarynav ul').hide();
        $('.navlogo').hide();   

  }, function() {
        $(this).removeClass("open");
        $('#nav').animate({width: 200},{queue:false, duration:300});
        $('.wrapify').animate({marginLeft: 200},{queue:false, duration:300});
        $('.primarynav ul').show();
        $('.navlogo').show(); 

  });
Run Code Online (Sandbox Code Playgroud)

dem*_*mux 28

$(document).ready(function() {
    // This will fire when document is ready:
    $(window).resize(function() {
        // This will fire each time the window is resized:
        if($(window).width() >= 1024) {
            // if larger or equal
            $('.element').show();
        } else {
            // if smaller
            $('.element').hide();
        }
    }).resize(); // This will simulate a resize to trigger the initial run.
});
Run Code Online (Sandbox Code Playgroud)

编辑:

或许这就是你所追求的:

$(document).ready(function() {
    if($(window).width() >= 1024) {
        $('a.expand').click();
    }
});
Run Code Online (Sandbox Code Playgroud)

如果宽度正确,这将在文档准备好时切换元素.