2011-04-27 34 views
0

你有沒有人知道如何jquery解除綁定函數woking?我有一個jquery驅動的相冊,我綁定一些東西在加載事件(改變圖像等)..但是當用戶很快在畫廊中漫步我想解除「舊」事件並僅在最後一張圖片上綁定加載事件。否則,用戶將會看到他在一小段時間內走過的每張圖片,而我只想顯示最後一張圖片。(performance,UX,..)我將每個加載的(未精確加載的,也是每個請求的)圖像存儲在一個數組中,所以每次我打電話給我的loadImage函數我打電話jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader") ,但它似乎不起作用,加載事件仍然存在(它有點奇怪,我無法在調試器中找到它,但我看到它:))有沒有從整個數組或整個js實例中解除事件的方式,除非通過foreach進行?從整個陣列的jQuery解除綁定事件

下面是一些代碼snippet..It是整體的LoadImage功能..

function loadImage(index , showLoading, callback){ 
    if(backgroundImages==undefined) backgroundImages=Array(); 
    //if(backgroundImages[window.location.hash][index]==undefined)backgroundImages[window.location.hash][index] = Array(); 

      jQuery("#galleryEl .sig_thumb a").each(function(pos , el) { 
      var wh_bg_b = getVierwportSize(); 
      if(index == pos){ 
        var bg_img_path_b = jQuery(el).attr('href'); 
        var size = 'max'; 
        if (bg_img_path_b != undefined) { 
        if ((wh_bg_b[0] < 1281) && (wh_bg_b[1] < 801)) { 
         size = 'min'; 
         bg_img_path_b = bg_img_path_b.substring(0, bg_img_path_b.lastIndexOf("/")) + "/1280/" + bg_img_path_b.substring(bg_img_path_b.lastIndexOf("/")+1); 
        } else if ((wh_bg_b[0] < 1441) && (wh_bg_b[1] < 901)) { 
         size = 'med'; 
         bg_img_path_b = bg_img_path_b.substring(0, bg_img_path_b.lastIndexOf("/")) + "/1440/" + bg_img_path_b.substring(bg_img_path_b.lastIndexOf("/")+1); 
        } 
       } 


        console.log("test"); 
        if(backgroundImages[bg_img_path_b]!=undefined){ 
         if(backgroundImages[bg_img_path_b].loaded=="true"){ 
         //console.log(index+" "+backgroundImages[window.location.hash][index][size]['loaded']); 
         if(typeof callback=='function'){ 
          callback.call(this, bg_img_path_b); 
         } } 
         else if(backgroundImages[bg_img_path_b].loaded=="loading"){ 
          jQuery(backgroundImages).unbind('load.backgroundImages'); 

          jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){ 
           backgroundImages[bg_img_path_b].loaded="true"; 
           if(typeof callback=='function'){ 
            callback.call(this, bg_img_path_b); 
            //console.log("loaded "+index); 
           } 
          }); 
         } 
        } 
        else{ 


        backgroundImages[bg_img_path_b]=new Image(); 
        backgroundImages[bg_img_path_b].src = bg_img_path_b; 
        backgroundImages[bg_img_path_b].loaded="loading"; 
        jQuery(backgroundImages).unbind('load.backgroundImages'); 

        jQuery(backgroundImages[bg_img_path_b]).bind('load.backgroundImages',function(){ 
         backgroundImages[bg_img_path_b].loaded="true"; 
         if(typeof callback=='function'){ 
          callback.call(this, bg_img_path_b); 
          //console.log("loaded "+index); 
         } 
        }); 
       } 
       console.log(backgroundImages[bg_img_path_b]); 

       //console.log(size); 
       //console.log(backgroundImages); 


      } 


    }); 

}

+1

如果你可以發表你的腳本了,這將使幫助更容易。另外這個工具對於調試事件很有用http://www.sprymedia.co.uk/article/Visual+Event – 2011-04-27 12:07:13

+0

所以它已經被修復了......我把指向我綁定在變量和nexr綁定之前的最後一個圖像unbind ..我必須將事件分成兩部分,才能將圖像設置爲加載(.loaded值)。現在一切正常..但是當我現在看到代碼的時候,開始的每個循環都不是正確的方法來做到這一點:)我通過使用eq(索引)函數來改變它:) – simekadam 2011-04-27 13:47:11

回答

0

您需要解除綁定不喜歡

jQuery(myArrayWithImages).unbind("load.backgroundImagesLoader") 

jQuery(myArrayWithImages).unbind(load.backgroundImagesLoader). 

只讀手冊人,你可以使用命名空間:

$('#foo').bind('click.myEvents', handler); 

$('#foo').unbind('click.myEvents'); 
+0

但我使用命名空間.. 我認爲存在問題,我綁定單個數組元素,然後從整個數組中解除綁定。因此,我將嘗試保留最後一個選擇器,我已綁定在一個然後我將能夠從最後插入(訪問)索引..它會更有效率...我希望它會工作 – simekadam 2011-04-27 12:49:34