
姑且使用这个标题来描述这个问题。
情况就是我的一个网站用 Twitter 那种居中显示图片一部分的方法来显示图片,我的原理就是前端载入图片时给 $('img') 定义 CSS
但是不是每次又都有效果,最后发现可能是因为图片的缓存原因,
我尝试的解决方法是
var el = $('img'); el.one("load", function() { /*do something, for me I wanna make the image center visible, so I make these moves, the parent of img element is overflow:hidden => var marginTop = 0; if($(this).height()>253){ marginTop = ($(this).height()-253) /2; marginTop = '-'+marginTop+'px'; } $(this).css('margin-top',marginTop); */ }).each(function() { if(this.complete) $(this).load(); }); 以前的实现方式:
var el = $('img'); el.on('load',function(){ //do something }) 似乎能解决问题,求高手正解 …
1 keakon 2014-11-23 22:52:52 +08:00 目测是你绑定的时机不对,在图片加载后才绑的。 |
2 airyland 2014-11-23 23:42:01 +08:00 绑定load事件再attr src属性。 图片缓存问题的话可以在src中加个随机字符串来解决。 |
3 loser 2014-11-25 09:22:37 +08:00 我眼花啦? LZ帖子后面跟了个doge头像? |