Commit 1be24b58048e5bdad008c6fb3aeec6463203fa78
1 parent
bfd3b718
[年会抽奖]调试头像尺寸
Showing
1 changed file
with
30 additions
and
2 deletions
mobile.js
... | ... | @@ -169,9 +169,37 @@ var action = { |
169 | 169 | var width = $(dom).width(); |
170 | 170 | var img = new Image(); |
171 | 171 | img.src = src; // 改变图片的src |
172 | - console.log('img: ', img); | |
173 | 172 | $(dom).on('load', function() { |
174 | - console.log('dom--onload'); | |
173 | + var imgWidth = $(dom).width(); | |
174 | + var imgHeight = $(dom).height(); | |
175 | + console.log('dom--onload: ', $(dom)); | |
176 | + console.log('width: ' + imgWidth); | |
177 | + console.log('height: ' + imgHeight); | |
178 | + var ratio = imgWidth / imgHeight; | |
179 | + if (ratio > 1) { | |
180 | + imgHeight = width; | |
181 | + imgWidth = width * ratio; | |
182 | + var margin = (imgWidth - width) / 2; | |
183 | + $(dom).css({ | |
184 | + width: imgWidth + 'px', | |
185 | + height: imgHeight + 'px', | |
186 | + 'margin-left': -margin + 'px' | |
187 | + }); | |
188 | + } else if (ratio < 1) { | |
189 | + imgWidth = width; | |
190 | + imgHeight = width / ratio; | |
191 | + var margin = (imgHeight - width) / 2; | |
192 | + $(dom).css({ | |
193 | + width: imgWidth + 'px', | |
194 | + height: imgHeight + 'px', | |
195 | + 'margin-top': -margin + 'px' | |
196 | + }); | |
197 | + } else { | |
198 | + $(dom).css({ | |
199 | + width: width + 'px', | |
200 | + height: width + 'px' | |
201 | + }); | |
202 | + } | |
175 | 203 | }); |
176 | 204 | if (img.complete) { // 判断是否有缓存 |
177 | 205 | console.log('img complete'); | ... | ... |