简体   繁体   English

最小高度的媒体查询 iOS Safari

[英]media queries with min-height on iOS Safari

I'm developing a photo browser with the Bootstrap framework.我正在使用 Bootstrap 框架开发照片浏览器。 It has a 4:3 aspect ratio that I'm trying to make responsive.它有一个 4:3 的宽高比,我正在尝试使其响应。 My basic approach is something like this:我的基本方法是这样的:

#carousel {
  width: 320px;
  height: 240px;
  ...
}

and then use media queries to support larger device widths and heights so that #carousel grows, but not any larger than the device, eg:然后使用媒体查询来支持更大的设备宽度和高度,以便 #carousel 增长,但不会比设备大,例如:

@media (min-width: 576px) and (min-height: 390px) {
  #carousel {
    min-width: 513px;
    min-height: 385px;
    border: 1px solid blue; /* test attribute */
  }
}

and so forth for larger devices.对于更大的设备等等。

This works fine overall and in the responsive testers built into Chrome and Safari. It does not work on my physical iPhone 13, however, which has a logical width/height of 390/844px.这在 Chrome 和 Safari 内置的响应式测试仪中整体运行良好。但是,它在我的物理 iPhone 13 上不起作用,它的逻辑宽度/高度为 390/844 像素。 The previous media selector should fire when the phone is in landscape, but it doesn't.以前的媒体选择器应该在手机横向时触发,但事实并非如此。 iPhone 13 in landscape mode doesn't fire until a much lower min-height in the media selector:横向模式下的 iPhone 13 在媒体选择器中的最小高度低得多之前不会触发:

@media (min-width: 576px) and (min-height: 300px) {
  #carousel {
    min-width: 513px;
    min-height: 385px;
    border: 1px solid blue; /* test attribute */
  }
}

Note that min-height in the media selector is much lower than min-height in the CSS definition.请注意,媒体选择器中的最小min-height远低于 CSS 定义中的min-height If the height actually was 300px then the carousel should not fit on the screen, but it looks just fine.如果高度实际上是 300 像素,那么轮播不应该适合屏幕,但它看起来很好。 Just not very efficient or sustainable.只是效率不高或不可持续。

I suspect what's going on is that Safari is subtracting the height of its address bar and tabs from the height value.我怀疑发生的事情是 Safari 从高度值中减去其地址栏和选项卡的高度。 In fact I'm sure of it, because I get different behavior depending on whether I have one tab open or several.事实上我很确定,因为我会根据打开一个或多个选项卡而得到不同的行为。 If there is only one tab open (and thus no tab bar) then I can get the media selector to fire at min-height: 333px but with multiple tabs I need to lower it to min-height: 300px .如果只有一个选项卡打开(因此没有选项卡栏),那么我可以让媒体选择器在min-height: 333px处触发,但是对于多个选项卡,我需要将其降低到min-height: 300px Neither one is actually correct, since if the user scrolls down in the browser then Safari hides the toolbar and makes the entire device height available (something similar happens on larger devices such as iPads).这两个实际上都不正确,因为如果用户在浏览器中向下滚动,则 Safari 会隐藏工具栏并使整个设备高度可用(类似的情况会发生在 iPad 等较大的设备上)。

Does anyone know how to query the effective display height from iOS Safari?有谁知道如何从iOS Safari查询有效显示高度?

I solved this—reluctantly—with a media query condition that specifically targets the iPhone 12 Pro/13 in landscape mode:我很不情愿地使用专门针对横向模式下的 iPhone 12 Pro/13 的媒体查询条件解决了这个问题:

@media (min-width: 576px) and (min-height: 300px),
    (device-width: 390px) and (orientation: landscape) {
  #carousel {
    min-width: 513px;
    min-height: 385px;
  }
}

This does what I want on iOS Safari. Unfortunately, it also applies to iOS Chrome, which has a different, smaller effective screen height.这在 iOS Safari 上做了我想要的。不幸的是,它也适用于 iOS Chrome,它具有不同的、更小的有效屏幕高度。 I haven't figured out an even remotely elegant solution to target iOS Safari without targeting iOS Chrome.我还没有想出一个更优雅的解决方案来定位 iOS Safari 而不以 iOS Chrome 为目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM