2011-09-06 94 views
1

嘗試在圖像加載,調整大小和隱藏一些元素後淡入容器,但遇到問題。在另一個回調中運行jQuery函數的問題

我的控制檯沒有顯示任何錯誤..但功能arn't執行

註釋掉的代碼工作完美。

測試地點:

http://brantley.dhut.ch/

的JavaScript:

/*jQuery(function($) { 
    $('.z').respond(); 
});*/ 

$(document).ready(function() { 
    //$('#two, #three, #four').hide(); 
    //$('#main').fadeIn('slow'); 

    $('.z').respond(function() { 
     $('#two, #three, #four').hide(); 
     $('#main').fadeIn('slow'); 
    }); 

    $('.z').click(function() { 
     var slide = $(this); 
     slide.fadeOut(600, function() { 
      slide.next().fadeIn(600); 
      slide.insertAfter('.z:last'); 
     }); 
    }); 
}); 

插件:

(function($){ 
    $.fn.respond = function() { 

     /* initialize function on window load and resize */ 
     $(document).ready(function() { 
      dimensions(); 
     }); 
     $(window).load(function() {  
      dimensions(); 
     }); 
     $(window).resize(function() { 
      dimensions(); 
     }); 

     /* declare affected elements */ 
     var pic = this 

     /* set image's height or width depending on the browser window's size */ 
     function dimensions() { 
      return pic.each(function() { 

      /* declare variables */ 
       var browserWidth = $(window).width(); 
       var imgRatio = $(this).width()/$(this).height() 
       var availableHeight = ($(window).height() - $('header').height() - $('footer').height() - 80) 
       var browserRatio = browserWidth/availableHeight 

       /* image sizing logic */ 
       if (browserRatio >= imgRatio) { 
        if (browserWidth > 640) { 
         /* if the browser is landscape and bigger than mobile, set the image's height to fill the available space */ 
         $(this).height(availableHeight).width('auto'); 
         //$('body').css('background', 'blue'); 
        } else { 
         /* it's mobile */ 
         $(this).width(browserWidth - 40).height('auto'); 
         //$('body').css('background', 'red'); 
        } 
       } else { 
        /* if the browser is portrait, set the image's width to fill the page */ 
        $(this).width(browserWidth - 40).height('auto'); 
        //$('body').css('background', 'green'); 
       } 

       /* horizontally center content */ 
       $(this).css('margin-left', (browserWidth - $(this).width())/2); 

      }); 

     }; 

    }; 
})(jQuery); 

回答

0

你的插件沒有定義的回調,這就是爲什麼什麼也沒有發生。

變化:$.fn.respond = function() {

到:$.fn.respond = function(callback) {

,並添加:callback();在插件結束。