推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
0x142857

图片由于缓存所以不会每次使用 .load() 都会有效果 doge

  •  
  •   0x142857 · Nov 23, 2014 · 4196 views
    This topic created in 4215 days ago, the information mentioned may be changed or developed.

    姑且使用这个标题来描述这个问题。

    情况就是我的一个网站用 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
        })
    

    似乎能解决问题,求高手正解 …

    3 replies    2014-11-25 09:22:37 +08:00
    keakon
        1
    keakon  
       Nov 23, 2014
    目测是你绑定的时机不对,在图片加载后才绑的。
    airyland
        2
    airyland  
       Nov 23, 2014
    绑定load事件再attr src属性。
    图片缓存问题的话可以在src中加个随机字符串来解决。
    loser
        3
    loser  
       Nov 25, 2014
    我眼花啦?
    LZ帖子后面跟了个doge头像?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   948 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 20:41 · PVG 04:41 · LAX 13:41 · JFK 16:41
    ♥ Do have faith in what you're doing.