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

zx兔子先生
  • 1.1k
@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.7k
3 个回答
https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia

请看文档

<!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

宣传栏