css - 是否可以根据 Bootstrap 中的屏幕大小选择图像?

标签 css twitter-bootstrap

我想根据显示尺寸显示不同的图像尺寸。例如,我想在手机显示屏上展示

/imageController/myimage/width/100 

/imageController/myimage/width/1000 

在大屏幕上。

这是可能的还是我需要先使用 jQuery 来检测屏幕尺寸?

非常感谢。

(不要因为拒绝这个问题而失去你的 XP 点数。如果我发现它没用,我会删除它)

最佳答案

您是否考虑过简单地使用 CSS 和 CSS 媒体查询来指定图像的内容,然后在屏幕尺寸低于 CSS 媒体查询中指定的阈值时更改图像的内容。

如果查看 bootstrap.css,您会发现该框架已经实现了许多 CSS 媒体查询。

我整理了一个快速测试,它在 Chrome 和 IE 中完美运行。

<!DOCTYPE html>
<html>
<head>
    <style>
        #my-image { content:url("[insert path to your full size image]"); }
        @media screen and (max-width: 640px) {
            #my-image { content:url("[insert path to your small size image]"); } /* Once the screen drops below 640px, the new image will show */
            img { border: solid 4px #000; }  /* I'm apply some an additional border to help illustrate when the screen width is below 640px */
        }
    </style>
</head>
<body>
    <img id="my-image" />
</body>
</html>

重要提示 您需要修改“content:url”才能运行上面的代码片段。

这是讨论此方法的另一篇 Stack Overflow 帖子:Switching CSS classes based on screen size

希望对您有所帮助。

关于css - 是否可以根据 Bootstrap 中的屏幕大小选择图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758269/

相关文章:

html - 将小于其 div 的边框居中

html - 应用外部 CSS 文件时出现问题

CSS - Bootstrap 中心 5 col-md-4

javascript - Bootstrap : Space between list group and Header on To-Do list?

jquery - 如何将 Bootstrap 中的下拉代码转换为悬停代码?

javascript - Glyphicon 图标未出现在首页和刷新后页面上

css - 根据浏览器窗口大小调整背景图像(HTML、CSS)

CSS 溢出框高度 100%

html - 如何在背景上制作模糊效果

asp.net-mvc - 如何在 MVC 5 项目中从 bootswatch 或 wrapbootstrap 实现主题?