Commit e68703dcd507b2d4a62bfc2779efc9480101df5a

Authored by zouyang0921
1 parent 14e2763d

[年会抽奖]调试头像尺寸

Showing 1 changed file with 38 additions and 72 deletions
mobile.js
... ... @@ -107,7 +107,7 @@ var action = {
107 107 if (action.userInfo) {
108 108 var userAvatar = action.userAvatar || defaultAvatar;
109 109 $('#home_user_avatar').attr('src', userAvatar);
110   - action.loadImage(userAvatar, '.user-avatar .avatar-img', '.user-avatar .avatar-img-wrap');
  110 + action.loadImage('.user-avatar .avatar-img', '.user-avatar .avatar-img-wrap');
111 111 $('#home_user_name').html(action.userInfo.realname);
112 112 $('#home_prize_no').html(action.userInfo.prize_no);
113 113 $('#home_table_no').html(action.userInfo.table_no);
... ... @@ -144,9 +144,14 @@ var action = {
144 144 localId: localIds.toString(), // 需要上传的图片的ID,由chooseImage接口获得
145 145 isShowProgressTips: 1, // 进度提示
146 146 success: function(res) {
147   - var imgHtml = '<img class="pic-avatar" src="' + localIds +'">';
148   - $('.pic-avatar-wrap').html(imgHtml);
149   - action.loadImage(localIds, '.pic-avatar', '.pic-avatar-wrap');
  147 + $('.pic-avatar').attr('src', localIds);
  148 + $('.pic-avatar').css({
  149 + width: '',
  150 + height: '',
  151 + 'margin-left': '',
  152 + 'margin-top': ''
  153 + });
  154 + action.loadImage('.pic-avatar', '.pic-avatar-wrap');
150 155 action.mediaId = res.serverId; // 返回图片的服务器端ID,即 mediaId
151 156 console.log('action.mediaId: ', action.mediaId);
152 157 $('#upload_unavailable').hide();
... ... @@ -166,77 +171,38 @@ var action = {
166 171 }
167 172 });
168 173 },
169   - loadImage: function(src, dom, parent) {
  174 + loadImage: function(dom, parent) {
170 175 var width = $(parent).width();
171   - console.log('parent-width: ', width);
172   - var img = new Image();
173   - img.src = src;
174   - if (img.complete) {
175   - console.log('img complete');
176   - } else {
177   - console.log('else');
178   - img.onload = function() {
179   - console.log('img onload');
180   - console.log('width: ' + img.width);
181   - console.log('height: ' + img.height);
182   - var ratio = img.width / img.height;
183   - if (ratio > 1) {
184   - img.height = width;
185   - img.width = width * ratio;
186   - var margin = (img.width - width) / 2;
187   - $(dom).css({
188   - 'margin-left': -margin + 'px'
189   - });
190   - } else if (ratio < 1) {
191   - img.width = width;
192   - img.height = width / ratio;
193   - var margin = (img.height - width) / 2;
194   - $(dom).css({
195   - 'margin-top': -margin + 'px'
196   - });
197   - } else {
198   - img.width = width;
199   - img.height = width;
200   - }
  176 + $(dom).on('load', function() {
  177 + var imgWidth = $(dom).width();
  178 + var imgHeight = $(dom).height();
  179 + var ratio = imgWidth / imgHeight;
  180 + if (ratio > 1) {
  181 + imgHeight = width;
  182 + imgWidth = width * ratio;
  183 + var margin = (imgWidth - width) / 2;
  184 + $(dom).css({
  185 + width: imgWidth + 'px',
  186 + height: imgHeight + 'px',
  187 + 'margin-left': -margin + 'px'
  188 + });
  189 + } else if (ratio < 1) {
  190 + imgWidth = width;
  191 + imgHeight = width / ratio;
  192 + var margin = (imgHeight - width) / 2;
  193 + $(dom).css({
  194 + width: imgWidth + 'px',
  195 + height: imgHeight + 'px',
  196 + 'margin-top': -margin + 'px'
  197 + });
  198 + } else {
  199 + $(dom).css({
  200 + width: width + 'px',
  201 + height: width + 'px'
  202 + });
201 203 }
202   - }
  204 + });
203 205 },
204   - // loadImage: function(dom, parent) {
205   - // var width = $(parent).width();
206   - // console.log('parent: ', width);
207   - // $(dom).on('load', function() {
208   - // var imgWidth = $(dom).width();
209   - // var imgHeight = $(dom).height();
210   - // console.log('dom--onload: ', $(dom));
211   - // console.log('width: ' + imgWidth);
212   - // console.log('height: ' + imgHeight);
213   - // var ratio = imgWidth / imgHeight;
214   - // if (ratio > 1) {
215   - // imgHeight = width;
216   - // imgWidth = width * ratio;
217   - // var margin = (imgWidth - width) / 2;
218   - // $(dom).css({
219   - // width: imgWidth + 'px',
220   - // height: imgHeight + 'px',
221   - // 'margin-left': -margin + 'px'
222   - // });
223   - // } else if (ratio < 1) {
224   - // imgWidth = width;
225   - // imgHeight = width / ratio;
226   - // var margin = (imgHeight - width) / 2;
227   - // $(dom).css({
228   - // width: imgWidth + 'px',
229   - // height: imgHeight + 'px',
230   - // 'margin-top': -margin + 'px'
231   - // });
232   - // } else {
233   - // $(dom).css({
234   - // width: width + 'px',
235   - // height: width + 'px'
236   - // });
237   - // }
238   - // });
239   - // },
240 206 // 上传头像
241 207 uploadImage: function() {
242 208 if (!action.mediaId) {
... ...