简体   繁体   English

如何在移动和桌面设备中显示不同的图像

[英]How to display different images in mobile and desktop devices

Hi can anyone tell me how to display differnet banner images in desktop and mobile devices.Here is my code.嗨,谁能告诉我如何在桌面和移动设备中显示不同的横幅图像。这是我的代码。

HTML: HTML:

<div class="row">
    <img src="image\bannerimages\Career.png" class="img-responsive careerpage">
    <h2 class="careerbannertext">LIFE AT TEKNOTRAIT</h2>
    <h2 class="careerbannertext1">Are you fanatically driven and ready to take on some of the best challenges the industry has to offer? </h2>
    <h2 class="careerbannertext2">Come join us,be inspired to do the best work of your life!</h2>       
</div>

Right now in my desktop version it is displaying this image i need to change another image in mobile version.现在在我的桌面版本中它正在显示这个图像我需要在移动版本中更改另一个图像。

you can try this one:你可以试试这个:

<picture>
   <source 
      media="(min-width: 650px)"
      srcset="images/img1.png">
   <source 
      media="(min-width: 465px)"
      srcset="images/img2.png">
   <img src="images/img-default.png" 
   alt="a cute kitten">
</picture>

the image tag is still there so you can put a default image for the other browser that doesnt support the picture tag.图片标签仍然存在,因此您可以为不支持图片标签的其他浏览器放置默认图片。

You can use source element by adding a media attribute with the value (orientation: portrait) for mobile devices and (orientation: landscape) for desktop devices.您可以通过为移动设备添加值为 (orientation: Portrait) 和为桌面设备添加值为 (orientation: Landscape) 的媒体属性来使用 source 元素。

Example:示例:

<picture>
   <source 
      media="(orientation: landscape)"
      srcset="testlandscape.png">
   <source 
      media="(orientation: portrait)"
      srcset="testportrait.png">
      
 <img src="testlandscape.png" width="100%" height="auto">
</picture>

Use this method instead.请改用此方法 I'm still learning it but it's a much better option than the others mentioned here.我仍在学习它,但它比这里提到的其他选择要好得多。 In addition to being done in HMTL only, it also doesn't download images in the background that aren't needed (eg it won't download the high-resolution desktop image and just hide it, like is done with CSS display: none).除了仅在 HMTL 中完成之外,它还不会在背景中下载不需要的图像(例如,它不会下载高分辨率桌面图像而只是将其隐藏,就像使用 CSS display: none 完成的那样) )。

I cannot remove my accepted answer, so please don't use it.我无法删除我接受的答案,所以请不要使用它。 The best way is using tag picture with sources, like Ron.Basco post:最好的方法是使用带有来源的标签图片,例如 Ron.Basco 帖子:

<picture>
   <source 
      media="(min-width: 650px)"
      srcset="images/img1.png">
   <source 
      media="(min-width: 465px)"
      srcset="images/img2.png">
   <img src="images/img-default.png" 
   alt="a cute kitten">
</picture>

My original accepted answer, which is not good, because images will be loaded on each device:我最初接受的答案并不好,因为图像将加载到每个设备上:

 .img-responsive.mobile { display: none; } @media only screen and (max-device-width: 480px) { .img-responsive { display: none; } .img-responsive.mobile { display: block; } }
 <div class="row"> <img src="image\\bannerimages\\Career.png" class="img-responsive careerpage"> <img src="image\\bannerimages\\Career-mobile.png" class="img-responsive careerpage mobile"> <h2 class="careerbannertext">LIFE AT TEKNOTRAIT</h2> ... </div>

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

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