Bootstrap根据设备更改图像

Sid*_*oor 11 twitter-bootstrap-3

我想在不同的设备(不是背景图像)中显示不同的图像.是否可以像以下一样使用bootstrap 3.x?

适用于大屏幕

<img src="large-image.jpg" />
Run Code Online (Sandbox Code Playgroud)

适用于中型设备

<img src="medium-image.jpg" />
Run Code Online (Sandbox Code Playgroud)

适用于小型设备

<img src="small-image.jpg" />
Run Code Online (Sandbox Code Playgroud)

等等.

提前致谢.

NB最后,我自己找到了一个很好的解决方案.请参阅下面接受的答案.

Sug*_*h G 12

试试这个:

<div class="row">
    <div class="col-xs-12 hidden-sm hidden-md hidden-lg">  
        <img class="img-responsive" src="layouts/layouts.PNG" />    <!--Mobile-->
    </div>
    <div class="hidden-xs col-sm-12 hidden-md hidden-lg">
        <img class="img-responsive" src="layouts/05.png" />   <!--Tab-->
    </div>
    <div class="hidden-xs hidden-sm col-md-12 hidden-lg">
        <img class="img-responsive" src="layouts/06.png" />   <!--Desktop-->
    </div>
    <div class="hidden-xs hidden-sm hidden-md col-lg-12">
        <img class="img-responsive" src="layouts/Layout_100.png" />  <!--Large-->
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

希望这可能有用!!

  • 我现在无法进入bootply来确认这一点,但这不会加载所有文件,但只显示相关的文件吗?我想(是的猜)OP想避免加载未查看的文件...... (5认同)
  • 我认为如果您正在加载大图像,中图像,小图像和xs图像,那么就会出现性能问题. (3认同)
  • 不,这不是一个好的解决方案,因为它会加载所有图像.2年后,我找到了解决方案,看到了我的答案. (3认同)
  • 根本没有在加载时调整大小,而是根据屏幕大小选择要加载的正确图像.就一次 (2认同)
  • 应该标记为正确的答案 (2认同)

Sid*_*oor 6

在@ EIChapo对这个2年前问题的评论之后,我已经搜索并阅读了很多关于这个解决方案的文章.当我发布这个答案时,除了IE之外的所有现代浏览器都支持<picture>标记.根据Dave Newton的文章,有一个防弹解决方案.

我们需要做的是如下:

<picture alt="fancy pants">
    <!-- loaded by browsers that support picture and that support one of the sources -->
    <source srcset="big.jpg 1x, big-2x.jpg 2x, big-3x.jpg" type="image/jpeg" media="(min-width: 40em)" />
    <source srcset="med.jpg 1x, med-2x.jpg 2x, big-3x.jpg" type="image/jpeg" />

    <!-- loaded by IE 8+, non-IE browsers that don’t support picture, and browsers that support picture but cannot find an appropriate source -->
    <![if gte IE 8]>
    <object data="fallback.jpg" type="image/jpeg"></object>
    <span class="fake-alt">fancy pants</span>
    <![endif]>

    <!-- loaded by IE 6 and 7 -->
    <!--[if lt IE 8]>
    <img src="fallback.jpg" alt="fancy pants" />
    <![endif]-->
</picture>

.fake-alt {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
}
Run Code Online (Sandbox Code Playgroud)


Ren*_*ler 5

使用<img>标签并在srcset属性中提供其他图像

<img src="small.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" alt="yah">
Run Code Online (Sandbox Code Playgroud)

这里有详细描述:

https://css-tricks.com/responsive-images-yure-just-changed-resolutions-use-srcset/