【发布时间】:2019-05-16 21:15:57
【问题描述】:
我正在尝试在现有 img 元素上预览个人资料照片我的问题是新的图像尺寸在 img 加载函数之外变得未定义。
如何将这些尺寸传递给服务器,以便正确调整 img 元素的大小?
我也尝试使用 Vuex Store 来传递维度,但结果相同。
我还有一个监听调整大小的函数,在用户更改窗口后,img 会正确调整大小,但是我试图在没有事件触发器的情况下执行此操作。即使我尝试使用 jQuery 手动触发调整大小事件,它也不会调整大小。我的印象是,在调整大小甚至刷新尺寸之前,新的 img 源的尺寸没有被正确设置?
<b-img id="profilePhoto" v-bind="profile" :src="this.photo" class="profilePhoto" @change="handleResize()"></b-img>
export default {
data() {
return {
errors:{},
profile: {},
fileDimensions: {},
photo: '',
form: this.$vform({
id: '',
name: '',
email: '',
password: '',
role: '',
bio: '',
photo: ''
})
}
}
}
getPhoto(e) {
let file = e.target.files[0];
if (typeof file !== 'undefined'){
let reader = new FileReader()
let limit = 1024 * 1024 * 2
if (file.size > limit) {
return false;
}
reader.onloadend = (file) => {
this.form.photo = reader.result
this.photo = this.form.photo
}
reader.readAsDataURL(file)
$("<img/>",{
load : function(){
// will return dimensions fine if i use alert here
this.fileDimensions = { width:this.width, height:this.height} ;
},
src : window.URL.createObjectURL(file)
});
// becomes undefined outside of the load functions
var aw = this.fileDimensions.width
var ah = this.fileDimensions.height
var ph = $('.profile').height()
var pw = $('.profile').width()
console.log(this.fileDimensions.width)
if (ah>aw){
this.profile = { width: ph, height: 'auto'}
} else if (aw>ah) {
this.profile = { width: 'auto', height: ph}
} else {
this.profile = { width: ph+10, height: pw+10}
}
}
}
我希望获得尺寸,以便我可以确定如何使用新的 src 设置 img 元素的宽度和高度属性,但是尺寸变得未定义。
【问题讨论】:
标签: image server-side-rendering nuxt.js