javascript - 为什么不申请时会调用 window.matchMedia 回调?

标签 javascript callback media-queries

我正在尝试将一组简单的媒体查询从 css 改编为 javascript。 css 定义按预期工作,但是,javascript 代码没有。

代码块如下所示:

  window.matchMedia("(min-width: 401px) and (max-width: 600px)")
  .addListener( function() {
    console.log("CALLBACK (max-width: 600px)");
    document.body.style.background = "red";
  });

根据宽度,我更改文档颜色。当我改变窗口的大小时,似乎调用了两个回调函数:一个匹配前一个宽度,一个匹配当前宽度。

我希望只调用与当前宽度匹配的那个。

例如,当我将浏览器大小从 window.innerWidth 1871 更改为 window.innerWidth 700 时,会发生以下情况:

window.innerWidth: 1871
CALLBACK (max-width: 800px)
CALLBACK (min-width: 801px)
window.innerWidth: 700

并且应用了 min-width 801px 的回调。

我做错了什么?

完整的测试代码在这里:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script>

window.onresize = afterWindowResize;
function afterWindowResize(){
  showSize();
}

function x(){
  showSize();

  window.matchMedia("(max-width: 400px)")
  .addListener( function() {
    console.log("CALLBACK (max-width: 400px)");
    document.body.style.background = "green";
  });

  window.matchMedia("(min-width: 401px) and (max-width: 600px)")
  .addListener( function() {
    console.log("CALLBACK (max-width: 600px)");
    document.body.style.background = "red";
  });

  window.matchMedia("(min-width: 601px) and (max-width: 800px)")
  .addListener( function() {
    console.log("CALLBACK (max-width: 800px)");
    document.body.style.background = "blue";
  });

  window.matchMedia("(min-width: 801px)")
  .addListener( function() {
    console.log("CALLBACK (min-width: 801px)");
    document.body.style.background = "gray";
  });
}

function showSize(){
  console.log("window.innerWidth: " + window.innerWidth); 
}

</script>
</head>
<body onload="x()">
</body>
</html>

最佳答案

如果你传递事件然后使用matches它会正常工作

window.matchMedia("...").addListener( function(e) {
    if (e.matches) {
       // do something
    }
}

或者您也应该能够使用 this,例如if (this.matches) {...}

堆栈片段

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script>
      window.onresize = afterWindowResize;

      function afterWindowResize() {
        showSize();
      }

      function x() {
        showSize();

        window.matchMedia("(max-width: 400px)")
          .addListener(function(e) {
            if (e.matches) {
              console.log("CALLBACK (max-width: 400px)");
              document.body.style.background = "green";
            }
          });

        window.matchMedia("(min-width: 401px) and (max-width: 600px)")
          .addListener(function(e) {
            if (e.matches) {
              console.log("CALLBACK (max-width: 600px)");
              document.body.style.background = "red";
            }
          });

        window.matchMedia("(min-width: 601px) and (max-width: 800px)")
          .addListener(function(e) {
            if (e.matches) {
              console.log("CALLBACK (max-width: 800px)");
              document.body.style.background = "blue";
            }
          });

        window.matchMedia("(min-width: 801px)")
          .addListener(function(e) {
            if (e.matches) {
              console.log("CALLBACK (min-width: 801px)");
              document.body.style.background = "gray";
            }
          });
      }

      function showSize() {
        console.log("window.innerWidth: " + window.innerWidth);
      }

    </script>
  </head>

  <body onload="x()">
  </body>

</html>

关于javascript - 为什么不申请时会调用 window.matchMedia 回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49984781/

相关文章:

html - 媒体查询斗争

javascript - Java脚本原型(prototype)中的方法未覆盖

python - 如何写一个简单的回调函数?

c# - 我如何持续监控新的 TCP 客户端?

javascript - 从带有错误值签名的回调中创建 promise

css - 媒体查询不起作用

css - 环境媒体功能支持

javascript - vue.js:如何处理同一元素上的 click 和 dblclick 事件

javascript - 无法识别键 "device-width;"的视口(viewport)参数值 "width"。内容被忽略

javascript - 如何使用 Angularjs 从 url 显示 youtube 缩略图