javascript - v-img 的 Vuetify 动态高度
问题描述
我有一个不是页面 100% 高度的容器,它是 80% 加上顶部和底部的 60px。所以里面的图片应该继承容器的高度。
使用<img />
和一些 CSS 很容易实现,但我希望添加一个带有加载微调器的模板,所以我开始使用<v-img>
它。
我可以看到我可以设置height
or max-height
,所以我做了一个方法来计算父高度减去 120px 以获得确切的图像高度,并且它按预期工作。
问题是当用户调整窗口大小时,我可以看到要调用的方法并更新值,但 vuetify 元素不响应新值,因此图像大于或小于容器:
<div id="photoWrapper" class="photo-wrapper">
<v-img
:src="photo.url"
:contain="true"
:max-height="wrapperHeight()"
>
<template v-slot:placeholder>
<v-layout fill-height align-center justify-center ma-0>
<v-progress-circular
indeterminate
color="tertiary"
></v-progress-circular>
</v-layout>
</template>
</v-img>
</div>
代码:
data() {
return {
mounted: false
}
},
mounted() {
this.mounted = true
window.addEventListener('resize', this.wrapperHeight)
this.wrapperHeight()
},
methods: {
wrapperHeight() {
if (!this.mounted) return
console.log(document.getElementById('photoWrapper').offsetHeight - 120)
const height = document.getElementById('photoWrapper').offsetHeight - 120
return height
}
}
该方法总是在屏幕调整大小时调用,但图像没有响应。我该怎么做?我还尝试将 移动wrapperHeight
为计算属性。
解决方案
我认为您必须${height}px
在wrapperHeight
方法中返回
推荐阅读
- typo3 - TYPO3 FileReference 存储库查询搜索通过文件名
- python - 图中沿 x 轴的非等距点
- javascript - AssertionError [ERR_ASSERTION]:必须指定任务函数 Gulp 中的错误
- android - android AnimationDrawable 有没有快速导入工具?
- javascript - 如何添加日期格式以从下午 4 点到上午 9 点 45 分禁用按钮
- firebase - 如何将图像形式的电话上传到 Firebase 存储?
- php - 如何读取协议缓冲区分隔的 I/O 函数
- python - scikit-learn 中的线性回归
- scala - Scala命名方法参数无法解析符号
- excel - 如何在 vba 中更改图表的背景图像?