【发布时间】:2021-08-11 10:43:59
【问题描述】:
我想以 4:3 的纵横比根据宽度按比例改变高度。这里的宽度是 100vw。所以我在挂载的钩子中通过 this.$refs.imageDiv.clientWidth 得到了宽度像素。现在,当我在 console.log() 中记录宽度和高度数时,它会完美记录。但是当我尝试使用计算属性动态更改高度时,它不会改变预期的高度。什么问题?
<template>
<div ref="imageDiv" :style="imageStyle" class="top-blog__photo">
<img
:src="require(`../assets/samplePics/${blog.photo}.jpg`)"
alt="Blog Photo"
/>
</div>
</template>
<script>
export default{
props: ['blog'],
data(){
iWidth: 0,
iHeight: 0,
},
computed: {
imageStyle(){
return{
width: iWidth+"px",
height: iHeight+"px",
}
}
},
mounted(){
if(this.iWidth === 0){
this.iWidth = this.$refs.imageDiv.clientWidth;
this.iHeight = this.iWidth / (4/3);
}
window.addEventListener("resize", ()=>{
this.iWidth = this.$refs.imageDiv.clientWidth;
this.iHeight = this.iWidth / (4/3);
})
}
}
</script>
<style lang="scss" scoped>
.top-blog{
&__photo{
img{
width: 100%;
height:100%;
object-fit: cover;
}
}
}
</style>
【问题讨论】:
标签: javascript html css vue.js