【问题标题】:Orientation change and screen.width方向改变和screen.width
【发布时间】:2016-09-02 18:18:33
【问题描述】:

我注意到更改设备方向(Android 手机)会导致 screen.width 发生更改。例如,在我的华为安卓手机上,在纵向位置screen.width == 360 像素,而在横向 640 像素。考虑到(至少据我所知)screen.width 拥有device-width 属性值,为什么下面的代码:

@media screen and (max-device-width:360px){
  div {
    background-color: green;
  }
}
@media screen and (min-device-width:361px){
  div {
    background-color: blue;
  }
}

不工作?

如果device-widthscreen.width 链接,它的值不应该像screen.width 那样随着实际设备方向而改变吗?

【问题讨论】:

  • jsbin.com/kusikelejo/edit?html,css,output 什么不起作用?似乎工作正常。尝试调整输出帧的大小。
  • 我不打扰桌面。正如我所写的,我正在移动 android 手机上测试代码。在上述device-width 的解决方案中不起作用 - 背景颜色不会随着设备方向的变化而变化。
  • 我刚刚使用 chrome 应用检查了手机上的 jsbin 链接。它正在以某种方式改变-

标签: css viewport


【解决方案1】:

device-width 是指设备本身的宽度,换句话说, 设备的屏幕分辨率

你想要这样的东西吗?

/* Portrait */
@media screen 
  and (device-width: 360px) 
  and (device-height: 640px) 
  and (-webkit-device-pixel-ratio: 3) 
  and (orientation: portrait) {
  div {
    background-color: green;
  }
}

/* Landscape */
@media screen 
  and (device-width: 360px) 
  and (device-height: 640px) 
  and (-webkit-device-pixel-ratio: 3) 
  and (orientation: landscape) {
  div {
    background-color: blue;
  }
}

【讨论】:

  • 抱歉,我对解决方案很熟悉你显示的。我只想知道screen.width 是否会随着设备方向的变化而变化,为什么screen.width 不会?
  • device-width 从设备获取静态值,screen.width 获取可以使用的空间。
  • 你可以在这里查看文档drafts.csswg.org/mediaqueries-3/#device-width
  • 所以screen.width == device-width 只是最初(比如变量初始化)和当设备方向改变时screen.width 改变但device-width as static 保持不变?跨度>
  • 好的,谢谢,但你是什么意思“设备宽度只改变方向”?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
相关资源
最近更新 更多