Image aspect ratio is preserved in Firefox, Chrome, and mobile Safari—but not in desktop Safari

huangapple go评论56阅读模式
英文:

Image aspect ratio is preserved in Firefox, Chrome, and mobile Safari—but not in desktop Safari

问题

我的图像具有不同的宽高比,如4:3、3:4、3:3、2:3和16:9。因此,我在CSS中没有设置固定的宽高比。相反,我设置了max-height: 94vh。这样,图像将适应屏幕。

当图像被点击时,我应用这个CSS,即当它具有active类时:

li.container {
  display: inherit;
  background-color: var(--fallback);
}

li.container:hover {
  cursor: pointer;
}

li.container.active {
  z-index: 1;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.9);
}

li.container.active img {
  max-height: 94vh;
  background: none;
}

我在Gatsby中使用gatsby-plugin-image。这是一个示例图像li

<li class="Photo-module--container--7f2bd active" tabindex="0" role="button">
  <div
    data-gatsby-image-wrapper=""
    class="gatsby-image-wrapper gatsby-image-wrapper-constrained"
  >
    <div style="max-width: 3717px; display: block">
      <img
        alt=""
        role="presentation"
        aria-hidden="true"
        style="max-width: 100%; display: block; position: static"
      />
    </div>
    <div
      aria-hidden="true"
      data-placeholder-image=""
      style="opacity: 0; transition: opacity 500ms linear 0s; object-fit: cover"
    ></div>
    <picture>
      <source type="image/webp" sizes="(min-width: 3717px) 3717px, 100vw" />
      <img
        data-main-image=""
        style="object-fit: cover; opacity: 1"
        sizes="(min-width: 3717px) 3717px, 100vw"
        decoding="async"
        loading="lazy"
        src="/static/7056e98c21e47b0856022508014b51ca/d3da5/DSCF3885.jpg"
        alt="Vannassen, Vassåsveien, Stavanger, 2023-02-11"
        width="3717"
        height="4956"
      />
    </picture>
    <noscript>
      <picture>
        <source type="image/webp" sizes="(min-width: 3717px) 3717px, 100vw" />
        <img
          width="3717"
          height="4956"
          data-main-image=""
          style="object-fit: cover; opacity: 0"
          sizes="(min-width: 3717px) 3717px, 100vw"
          decoding="async"
          loading="lazy"
          src="/static/7056e98c21e47b0856022508014b51ca/d3da5/DSCF3885.jpg"
          alt="Vannassen, Vassåsveien, Stavanger, 2023-02-11"
        />
      </picture>
    </noscript>
  </div>
  <!-- 绝对位置的按钮 -->
</li>

这在除了桌面Safari之外的所有浏览器中都可以正常工作。

例如,在Firefox中:

Image aspect ratio is preserved in Firefox, Chrome, and mobile Safari—but not in desktop Safari

但在桌面Safari中,会出现以下问题:

Image aspect ratio is preserved in Firefox, Chrome, and mobile Safari—but not in desktop Safari

似乎桌面Safari尊重了max-height: 94vh,但没有尊重宽高比。它只是放大了图像。为什么?

英文:

My images are set with different aspect ratios, such as 4:3, 3:4, 3:3, 2:3, and 16:9. I therefore don't set a fixed aspect ratio in the CSS. Instead, I set a max-height: 94vh. That way, the images will fit the screen neatly.

I apply this CSS when the image is clicked—when it has the active class:

li.container {
  display: inherit;
  background-color: var(--fallback);
}

li.container:hover {
  cursor: pointer;
}

li.container.active {
  z-index: 1;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.9);
}

li.container.active img {
  max-height: 94vh;
  background: none;
}

I use Gatsby with the gatsby-plugin-image. Here's an example image li:

<li class="Photo-module--container--7f2bd active" tabindex="0" role="button">
  <div
    data-gatsby-image-wrapper=""
    class="gatsby-image-wrapper gatsby-image-wrapper-constrained"
  >
    <div style="max-width: 3717px; display: block">
      <img
        alt=""
        role="presentation"
        aria-hidden="true"
        style="max-width: 100%; display: block; position: static"
      />
    </div>
    <div
      aria-hidden="true"
      data-placeholder-image=""
      style="opacity: 0; transition: opacity 500ms linear 0s; object-fit: cover"
    ></div>
    <picture>
      <source type="image/webp" sizes="(min-width: 3717px) 3717px, 100vw" />
      <img
        data-main-image=""
        style="object-fit: cover; opacity: 1"
        sizes="(min-width: 3717px) 3717px, 100vw"
        decoding="async"
        loading="lazy"
        src="/static/7056e98c21e47b0856022508014b51ca/d3da5/DSCF3885.jpg"
        alt="Vannassen, Vassåsveien, Stavanger, 2023-02-11"
        width="3717"
        height="4956"
      />
    </picture>
    <noscript>
      <picture>
        <source type="image/webp" sizes="(min-width: 3717px) 3717px, 100vw" />
        <img
          width="3717"
          height="4956"
          data-main-image=""
          style="object-fit: cover; opacity: 0"
          sizes="(min-width: 3717px) 3717px, 100vw"
          decoding="async"
          loading="lazy"
          src="/static/7056e98c21e47b0856022508014b51ca/d3da5/DSCF3885.jpg"
          alt="Vannassen, Vassåsveien, Stavanger, 2023-02-11"
        />
      </picture>
    </noscript>
  </div>
  <!-- button with absolute position -->
</li>

This works fine in all browsers except for desktop Safari.

In Firefox, for instance:

Image aspect ratio is preserved in Firefox, Chrome, and mobile Safari—but not in desktop Safari

But in desktop Safari, this happens:

Image aspect ratio is preserved in Firefox, Chrome, and mobile Safari—but not in desktop Safari

It seems desktop Safari respects the max-height: 94vh, but it doesn't respect the aspect ratio. It just zooms in all the way.

Why?

答案1

得分: 1

li.container.active img {
  object-fit: contain !important;
}
英文:

for fix aspect ratio in safari desktop we can use object-fit css property :

li.container.active img {
  object-fit: contain !important;
}

huangapple
  • 本文由 发表于 2023年2月14日 01:51:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定