vue图片超出屏幕尺寸自动适应图片比例缩放
<div>
<el-button class="blue-btn"
@click="open">弹出dialog</el-button>
<el-dialog :show-close="false"
:before-close="close"
:visible.sync="dialogVisible"
custom-class="hint"
top="50px"
:width="width + 'px'"
center>
<div class="imgwrapper">
<el-button class="dark-grey-btn left"
@click="left">左</el-button>
<img :src="imgList[index].imgUrl"
class="img"
ref="imgs"
:style="{width: imgWidth + 'px', height: imgHeigth + 'px'}"
@load="onload" />
<el-button class="dark-grey-btn right"
@click="right">右</el-button>
</div>
<span slot="footer"
class="dialog-footer">
<el-button class="dark-grey-btn"
@click="close">关闭</el-button>
</span>
</el-dialog>
</div>
export default {
name: 'music',
data () {
return {
index: 0,
width: '',
imgWidth: '',
imgHeigth: '',
dialogVisible: false,
imgList: [
{
imgUrl: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3681705955,4153554780&fm=26&gp=0.jpg'
},
{
imgUrl: 'http://192.168.188.127:8711/upload/veterans_dev/manageImage/11.jpg'
},
{
imgUrl: 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1093300176,3481197507&fm=26&gp=0.jpg'
},
{
imgUrl: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2156271884,4153213391&fm=26&gp=0.jpg'
}
]
}
},
computed: {
winWidth: function () {
return document.documentElement.clientWidth || document.body.clientWidth;
},
winHeight: function () {
return document.documentElement.clientHeight || document.body.clientHeight;
}
},
created () {
},
mounted () {
},
methods: {
onload (e) {
let imgW = e.target.naturalWidth
let imgH = e.target.naturalHeight
let maxWidth = 1240
if (imgW < maxWidth && imgH < this.winHeight - 251) {
this.width = imgW + 50
this.imgWidth = imgW
this.imgHeigth = imgH
} else {
if ((maxWidth - 50) / (this.winHeight - 251) <= imgW / imgH) {
this.width = maxWidth
this.imgHeigth = (maxWidth - 50) / (imgW / imgH)
this.imgWidth = maxWidth - 50
} else {
this.imgWidth = (this.winHeight - 251) * (imgW / imgH)
this.width = this.imgWidth + 50
this.imgHeigth = this.winHeight - 255
}
}
},
close () {
this.dialogVisible = false
},
open () {
this.dialogVisible = true
},
right () {
if (this.index < this.imgList.length - 1) {
this.index++
}
},
left () {
if (this.index > 0) {
this.index--
}
}
}
}