繁体   English   中英

根据屏幕大小更改图片

[英]Change the picture depending on the screen size

我的页面上有一张图片

<div class="title-image">
     <img src="{{ $article->mainImage }}">
</div>

假设我的移动版本在屏幕尺寸小于 768px 时启动

而且应该已经有这样的图了

<div class="title-image">
     <img src="{{ $article->mainImageMobile }}">
</div>

是否可以在页面的此处执行此操作,以便图片根据屏幕而变化? 还是只需要包含 css?

您必须已经为此 HTML 拥有不同的图像尺寸。

<img srcset="{{ $article->mainImageMobile }} 120w,
             {{ $article->mainImage }} 278w"
     sizes="(max-width: 710px) 120px,
            278px">

srcset是图像集。 w中使用单位。
sizes是一组媒体条件。

阅读有关MDNW3Schools的更多信息。

您可以使用 css 来实现这一点,方法是使用显示块而不是取决于设备大小

HTML:

<div class="title-image">
     <img class="laptop" src="{{ $article->mainImage }}">
    <img class="mobile" src="{{ $article->mainImageMobile }}">

</div>

css:

.mobile{
  display:none;
}

@media only screen 
  and (max-width: 768px)
  {
    
    .laptop{
      display:none !important;
    }
    
    .mobile{
      display:block !important;
    }

}

暂无
暂无

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

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