javascript - 我可以根据浏览器的缩放调整图像大小吗?
问题描述
我在一个网站上工作,我注意到当我缩小浏览器时,除了图像之外的所有内容都会调整大小。无论浏览器的 100% 还是 50%,图像都保持相同的大小。有没有办法可以根据浏览器的缩放对图像进行编码以调整大小?
我正在为这个网站使用 HTML5、CSS 和 JavaScript。并且图像是使用 JavaScript 渲染的。
function get_attrs() {
// change the avatar based on requirements
if (convStyle == 'dominant') {
$("#robo-image").attr("src", "images/avatar/D-Robo.png");
$(".user-description").prepend("<h3>Max</h3>");
} else if (convStyle == 'submissive') {
$("#robo-image").attr("src", "images/avatar/S-Robo.png");
$(".user-description").prepend("<h3>Linus</h3>");
}
}
.robot-section {
overflow: hidden;
padding: 3rem 0 3rem 2rem;
/*padding: 3rem 0rem;*/
}
.robot-section {
overflow: hidden;
padding: 3rem 0 3rem 2rem;
/*padding: 3rem 0rem;*/
}
.robot-content-bottom {
overflow-x: hidden;
overflow-y: auto;
max-width: 100%;
/*position: absolute;*/
/*bottom: 5%;*/
}
<div class="col-sm-6 col-md-4 col-lg-3 robot-section">
<div class="robot-content-top">
<div id="messageContainer"></div>
</div>
<div class="robot-content-bottom text-center">
<img id="robo-image" alt="avatar" class="avatar-img">
<div class="user-description">
<h4>Your personal financial advisor</h4>
</div>
<!--<div class="user-description">-->
<!--<!–<h3 style="margin-top: 30px;">Ethan</h3>–>-->
<!--<!–<h5>Your personal financial advisor</h5>–>-->
<!--</div>-->
</div>
</div>
解决方案
我相信您正在处理以百分比指定的宽度或高度问题。
如果您将宽度指定为百分比 (100%),则图像将始终为浏览器的 100% 宽度,并且不会随缩放而缩小/增长。
具有固定宽度和高度的图像会缩小/增长,而以百分比指定宽度的图像不会。
<img id="image" src="" width="622" height="350">
<img id="image2" src="" width="100%">
jsfiddle上的示例。
推荐阅读
- css - 对于 inline-flex 容器,`flex: 0 0 120px` 不起作用,而 `width: 120px` 起作用。哪些 flexbox 规则指定了这种行为?
- typescript - 从具有指定类型的泛型函数中的道具是条件类型的类型中提取所有道具的最佳方法
- visual-studio - 无法使用 Visual Studio 2019 部署我的 Azure Web 应用程序
- java - Java用密码加密文件
- python - 如何使用 matplotlib 在一个轴上绘制带有日期的线条
- linux - 在 Linux 中比较 2 个文件夹中每个字节的文件字节并查找重复项
- forms - 如何防止更新 CakePHP 3 beforesave() 中的字段?
- python - 如何根据 urls.py Django 中某个查询参数的值将 URL 路由到单独的视图?
- excel - 运行时错误 91 VBA - 尝试设置工作表但它一直说运行时错误?
- html - 具有自定义设计的侧导航栏