想通过js判断css的媒体查询 出错

@media screen and (min-width: 360px) and (max-width: 375px){
    .ejiao_description .form form p{
        margin-bottom: 11px;
    }
}

css中媒体查询是这么写的
js

var screen1 = window.matchMedia('@media  (min-width: 360px) and (max-width: 375px)');
    if (screen1.matches){
        XXXXXX.....
    }
    else{
        XXXX....
    }

但是现在没起作用 请问哪里错了么

回复
阅读 4.8k
3 个回答
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        @media screen and (min-width: 360px) and (max-width: 375px){
            .aa .form form p{
                margin-bottom: 11px;
            }
        }
    </style>
</head>
<body>
<!-- 想通过js判断css的媒体查询 出错 -->
<div class="aa">
    <div class="form">
        <form>
            <p>123123</p>
        </form>
    </div>
</div>
</body>
<script>
    var screen1 = window.matchMedia('(min-width: 360px) and (max-width: 375px)');
    console.log( screen1 );
    if (screen1.matches){
        console.log( 1 );
    }
    else{
        console.log( 2 );
    }
</script>
</html>

window.matchMedia('(min-width: 360px) and (max-width: 375px)');,没@media

https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia

请看文档

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏