【问题标题】:window.matchMedia not changing even if media queries are present即使存在媒体查询,window.matchMedia 也不会改变
【发布时间】:2013-06-01 02:07:26
【问题描述】:

有一个带有这些规则的 CSS 文件:

@media screen and (orientation:portrait) {
  .foo {}
}
@media screen and (orientation:landscape) {
  .foo {}
}

然后我得到了这个脚本,在 orientationchange 上运行:

function checkOr(){
    if (window.matchMedia("(orientation:portrait)").matches) {
      console.log('portrait ' + window.orientation);
    } else {
      console.log('landscape '+ window.orientation);
    }
}

但是matchMedia总是返回页面的初始状态,当window.orientation返回正确的值时:

portrait -90
portrait 0
portrait -90

在带有最新 iOS 更新的 iPhone 5 和 iPad 4 上进行测试。

我做错了什么? Webkit bugtracker 说每个媒体查询都必须有规则,但这不是我的情况。

【问题讨论】:

标签: javascript css webkit media-queries matchmedia


【解决方案1】:

您如何添加事件以在orientationchange 上调用函数?我刚刚在我的 iPad 上尝试过,它似乎工作正常:http://jsbin.com/ewixuc/2

function checkOr(){
    if (window.matchMedia("(orientation:portrait)").matches) {
      alert('portrait ' + window.orientation);
    } else {
      alert('landscape '+ window.orientation);
    }
}

window.addEventListener("orientationchange", checkOr);

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多