2012-08-13 211 views
0

有没有一种方法使用jQuery添加事件监听器,就像在AS 3.0中可用的一样?例如,如果您加载图像(将其不透明度设置为零以便它不出现在屏幕上),那么jQuery是否具有类似于onComplete事件侦听器的功能,该侦听器在负载完成时侦听?一旦加载成功加载,你可以触发另一个函数,将其不透明度再次变回1。jQuery事件监听器

我已经看到了一些已经编写的插件,但我想问一个问题,看看是否有人在没有使用第三方插件的情况下找到解决方案。

谢谢。

+0

你的意思是类似['.load()'](http://api.jquery.com/load-event/)? – MrOBrian 2012-08-13 23:03:15

回答

2

像这样的东西?

$(function(){ 
    $("img").hide().load(function(){ 
     $(this).fadeIn(); 
    }); 
}); 

第一行是简写$(document).ready(function(){

然后我们只需选择所有img元素,隐藏起来,并添加一个load处理程序,他们渐渐消逝他们进来。

尽管如此,如果缓存上述可能会证明是麻烦。以下将解决这个问题。

$(function(){ 
    $("img").hide().each(function(){ 
     if(!$(this).width()){ // image dimensions are unavailable if it's not loaded 
      $(this).load(function(){ 
       $(this).fadeIn(); 
      }); 
     }else{ //if the image is loaded already 
      $(this).fadeIn(); 
     } 
    }); 
}); 
+0

谢谢ahren,我会给你一个例子。 – user1563944 2012-08-13 23:16:06