Javascript媒体查询

Javascript Media Query

本文关键字:查询 媒体 Javascript      更新时间:2023-09-26

我正在尝试使用JavaScript实现媒体查询,因为我正在设计的浏览器不支持它。我不太确定我做错了什么,我已经用尽了我的选择,无论如何,这是我的代码

if(window.innerWidth > 1000 && window.innerWidth < 1300) {
    document.getElementById('id').style.height="50px";
 } 

如果宽度大于1000和1300,则要求浏览器更改元素的高度。这行不通。您可以尝试这样做:

if(window.innerWidth > 1000) {
    document.getElementById('id').style.height="50px";
} 

我想你问的是:

if(window.innerWidth > 1000 && window.innerHeight > 1300) {
    document.getElementById('id').style.height="50px";
} 
为了获得更好的结果,您应该添加一个事件侦听器
window.onresize = function(event) {
    if(window.innerWidth > 1000 && window.innerHeight > 1300) {
        document.getElementById('id').style.height="50px";
    }
};