Commit 1510ec841513f7ab38fab335202a995cd827a4db
1 parent
349e6114
[年会抽奖]修复上传头像变形
Showing
1 changed file
with
51 additions
and
28 deletions
mobile.js
... | ... | @@ -145,7 +145,7 @@ var action = { |
145 | 145 | isShowProgressTips: 1, // 进度提示 |
146 | 146 | success: function (res) { |
147 | 147 | $('.pic-avatar').attr('src', localIds); |
148 | - action.loadImage('.pic-avatar'); | |
148 | + action.loadImage('.pic-avatar', localIds); | |
149 | 149 | action.mediaId = res.serverId; // 返回图片的服务器端ID,即 mediaId |
150 | 150 | console.log('action.mediaId: ', action.mediaId); |
151 | 151 | $('#upload_unavailable').hide(); |
... | ... | @@ -165,35 +165,58 @@ var action = { |
165 | 165 | } |
166 | 166 | }); |
167 | 167 | }, |
168 | - loadImage: function(dom) { | |
168 | + loadImage: function(dom, src) { | |
169 | 169 | var width = $(dom).width(); |
170 | - var height = $(dom).height(); | |
171 | - var imgWidth = '', imgHeight = ''; | |
172 | - console.log('width: ' + width, 'height: ' + height); | |
173 | - var ratio = width / height; | |
174 | - if (ratio > 1) { | |
175 | - imgHeight = width; | |
176 | - imgWidth = width * ratio; | |
177 | - var margin = (imgWidth - width) / 2; | |
178 | - $(dom).css({ | |
179 | - width: imgWidth + 'px', | |
180 | - height: imgHeight + 'px', | |
181 | - 'margin-left': -margin + 'px' | |
182 | - }); | |
183 | - } else if (ratio < 1) { | |
184 | - imgWidth = width; | |
185 | - imgHeight = width / ratio; | |
186 | - var margin = (imgHeight - width) / 2; | |
187 | - $(dom).css({ | |
188 | - width: imgWidth + 'px', | |
189 | - height: imgHeight + 'px', | |
190 | - 'margin-top': -margin + 'px' | |
191 | - }); | |
170 | + var img = new Image(); | |
171 | + img.src = src; // 改变图片的src | |
172 | + if (img.complete) { // 判断是否有缓存 | |
173 | + console.log('img complete'); | |
174 | + console.log('width: ', width); | |
175 | + console.log('img-width: ' + img.width, 'img-height: ' + img.height); | |
176 | + var ratio = img.width / img.height; | |
177 | + if (ratio > 1) { | |
178 | + img.height = width; | |
179 | + img.width = width * ratio; | |
180 | + var margin = (img.width - width) / 2; | |
181 | + $(dom).css({ | |
182 | + 'margin-left': -margin + 'px' | |
183 | + }); | |
184 | + } else if (ratio < 1) { | |
185 | + img.width = width; | |
186 | + img.height = width / ratio; | |
187 | + var margin = (img.height - width) / 2; | |
188 | + $(dom).css({ | |
189 | + 'margin-top': -margin + 'px' | |
190 | + }); | |
191 | + } else { | |
192 | + img.width = width; | |
193 | + img.height = width; | |
194 | + } | |
192 | 195 | } else { |
193 | - $(dom).css({ | |
194 | - width: width + 'px', | |
195 | - height: width + 'px' | |
196 | - }); | |
196 | + img.onload = function() { | |
197 | + console.log('img onload'); | |
198 | + console.log('width: ', width); | |
199 | + console.log('img-width: ' + img.width, 'img-height: ' + img.height); | |
200 | + var ratio = img.width / img.height; | |
201 | + if (ratio > 1) { | |
202 | + img.height = width; | |
203 | + img.width = width * ratio; | |
204 | + var margin = (img.width - width) / 2; | |
205 | + $(dom).css({ | |
206 | + 'margin-left': -margin + 'px' | |
207 | + }); | |
208 | + } else if (ratio < 1) { | |
209 | + img.width = width; | |
210 | + img.height = width / ratio; | |
211 | + var margin = (img.height - width) / 2; | |
212 | + $(dom).css({ | |
213 | + 'margin-top': -margin + 'px' | |
214 | + }); | |
215 | + } else { | |
216 | + img.width = width; | |
217 | + img.height = width; | |
218 | + } | |
219 | + } | |
197 | 220 | } |
198 | 221 | }, |
199 | 222 | // 上传头像 | ... | ... |