【发布时间】:2019-11-25 16:36:14
【问题描述】:
我正在构建一条聊天消息,我想在文本气泡中放置一张图片。气泡具有固定宽度,应根据图像纵横比缩放其高度。
我无法缩放 div 以保持 img 的纵横比。找到的所有答案都使用 <img> 标签,而我使用的是 div 并且没有一个解决我的问题。
这是期望的结果:
我所取得的结果要么是非常小的img(如果我不施加任何垂直或水平尺寸,则为空对话气泡的大小)或长的img,如果我施加宽度并保持高度动态.
.speech-right-wrapper {
max-width: 90%;
display: flex;
flex-direction: column;
}
speech-right-message-container {
display: flex;
justify-content: flex-end;
align-items: flex-start;
}
.speech-right-bubble {
position: relative;
background: rgb(154, 226, 255);
box-shadow: 0px 1px 2px gray;
border-radius: 15px 0 15px 15px;
display: flex;
}
.bubble-background {
width: 200px;
height: auto;
overflow: hidden;
}
.bubble-background-img {
position: absolute;
border-radius: 15px 0 15px 15px;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: hidden;
background: no-repeat;
background-size: cover;
background-position: center;
}
<div class="speech-right-wrapper">
<div class="speech-right-message-container">
...
<div class="speech-right-bubble">
<div class="bubble-background">
<div class="bubble-background-img" v-bind:style="{ 'background-image': 'url(' + require('../assets/img/profiles/' + storyPath + bubble.bubble_background) + ')' }" v-bind:alt="bubble.bubble_background"></div>
</div>
...
</div>
...
</div>
</div>
更新
为了清楚起见,我需要将<div class="bubble-background-img"> 的宽度固定(比如200px)和height: auto 以保持图像纵横比;我需要 <div class="speech-right-bubble"> 缩放以适应里面的图像。
另外,不幸的是我需要使用<div> 标签,因为v-bind:src 似乎不适用于本地图像。如果我这样做:
<img class="bubble-background-img" v-bind:src="'../assets/img/profiles/' + storyPath + bubble.bubble_background"/>
【问题讨论】:
-
你不能使用
<img>标签还是说你看到的使用<img>标签的解决方案不起作用? -
@Dadboz,我不能分享完整的代码。
-
@Victor,该应用程序在 Vue 中,我无法使用 img 标签渲染图像。经过多次尝试,我发现 'v-bind:style' 和 'require' 可以正常工作。
-
不
v-bind:src不适用于您的 url 变量?