2013-01-11 56 views
0

我的控制檯出現錯誤=>未捕獲的TypeError:無法讀取null的屬性'maxHeight'。Slider bug javascript

嗯,我認爲這是與jQuery版本的問題,但我不知道如何解決這個問題,如果你能幫助我。 jQuery的版本:1.4.2

下面的代碼:

$.fn.slider = function(options, callback) { 

if ($.isFunction(options)) { 
    callback = options; 
    options = null; 
} 
options = $.extend($.fn.slider.defaults,options); 

return this.each(function() { 

    var prt = $(this), itms = prt.find(options.itms), w = prt.width(), max = itms.length, mH = prt.data("options").maxHeight, p = 0, autoStart = prt.data("options").autoStart || options.autoStart, s = (prt.data("options").speed || options.speed) * 1000, t=s/360; 

    if(max<2) return; 

    if(prt.find('>.inner').length==1) { 
     prt.find('>.inner').css({'width':w+'px'}); 
     itms.css({'width':w+'px'}); 
     return; 
    } 
    prt.wrapInner('<div class="inner" />'); 

    prt.css({'min-height':mH+'px'}); 
    prt.find('>.inner').css({'height':mH+'px','width':w+'px'}); 
    prt.append('<span class="corners"><span class="c1" /><span class="c2" /><span class="c3" /><span class="c4" /></span><span class="borders"><span class="b1" /><span class="b2" /><span class="b3" /><span class="b4" /></span>'); 

    itms.css({'line-height':mH-2+'px','width':w+'px','min-height':mH+'px'}); 
    /*@ 
    if(ie_rv < 8) { 
    itms.each(function(){ 
    var ieImg = $(this).find('img:first-child'); 
    var h=(mH-ieImg.height())/2; 
    ieImg.css({'margin-top':h+'px'}); 
    }); 
    } 
    @*/ 

    var c = prt.find(' > .current'); 
    var cId = (c.length>0) ? c.index() : 0; 

    var iterators = ''; 
    itms.each(function(){ 
     var i = $(this).index(); 
     iterators += '<button type="button" title="Slide '+(i+1)+'">&bull;</button>'; 
    }); 
    $('<div class="sliderNav"><button type="button" class="prev">&laquo;</button><span class="iterators">'+iterators+'</span><button type="button" class="next">&raquo;</button><button class="playPause">&#x25BA;</button><span class="clock" /></div>').appendTo(prt); 
    var iteratorBtns = prt.find('.iterators button'); 
    var playPause = prt.find('.playPause'); 
    var clock = prt.find('.clock'); 
    iteratorBtns.eq(cId).addClass('on'); 
    prt.find('.next').bind('click',function(){ 
     hold(); 
     shiftItem('fwd','static'); 
    }); 
    prt.find('.prev').bind('click',function(){ 
     hold(); 
     shiftItem('back','static'); 
    }); 
    iteratorBtns.bind('click',function(){ 
     hold(); 
     shiftItem('unknown','static',$(this).index()); 
    }); 
    playPause.bind('click',function(){ 
     toggle(); 
    }); 


    prt.bind('mouseenter',function(){ 
     hold(); 
    }); 
    prt.bind('mouseleave',function(){ 
     if(autoStart==true) 
      toggle(); 
    }); 


    // first in 
    itms.eq(cId).fadeTo(0,0).animate({'left':p,'opacity': 1},0,function(){ 
     $(this).css({'filter': 'none'}) 
    }); 

    prt.data('status','idle'); 
    prt.data('playPause','pause'); 

    if(autoStart==true) 
     toggle(); 

    function shiftItem(direction,type,n){ 
     holdItem(); 
     var w = prt.width(); 
     if(prt.data('status')=='moving') return; 
     prt.data('status','moving'); 
     if(direction=='unknown') { 
      if(n==cId) { 
       prt.data('status','idle'); 
       if(type!='static') play(); 
       return; 
      } 
      direction = (n>cId) ? 'fwd' : 'back'; 
     } 
     else 
      var n = (direction == 'fwd') ? (cId+1 == max) ? 0 : cId+1 : (cId-1 < 0) ? max-1 : cId-1; 

     var sS = (direction == 'fwd') ? w+(2*parseInt(p,10)) : -w-(2*parseInt(p,10)); 

     itms.eq(cId).animate({'opacity': 0},700,function(){}); 
     itms.eq(n).fadeTo(0,0.5).animate({'opacity': 1},700,function(){ 
      $(this).css({'filter': 'none'}); 
      iteratorBtns.removeClass('on'); 
      iteratorBtns.eq(n).addClass('on'); 
      cId=n; 
      if(type!='static') { 
       play(); 
      } 
      prt.data('status','idle'); 
     }); 

    } 

    var clockMax = 572, clockWidth = 26, clockSlices = 22, clockSpeed = s/clockSlices, currentPosition = 0; 

    function setClock() { 
     resetClock(); 

     clock.data('timer',setTimeout(function(){ 
      shiftClock(); 
     },clockSpeed)); 

    } 

    function shiftClock(){ 
     currentPosition -= clockWidth; 
     clock.css({'background-position': currentPosition+'px 0'}); 
     clock.data('timer',setTimeout(function(){ 
      shiftClock(); 
     },clockSpeed)); 
    } 

    function resetClock() { 
     clearTimeout(clock.data('timer')); 
     currentPosition = 0; 
     clock.css({'background-position':'0 0'}); 
    } 

    function hold(){ 
     holdItem(); 
     prt.data('playPause','pause') 
     playPause.html('&#x25BA;').removeClass('paused'); 
     resetClock(); 
    } 

    function play() { 
     if(prt.data('playPause')=='play') { 
      prt.data('timer',setTimeout(function(){ 
       shiftItem('fwd','dynamic'); 
      },s)); 
      setClock(); 
     } 
    } 

    function toggle(){ 

     var state = (prt.data('playPause')=='pause') ? 'play' : 'pause'; 

     if(state=='play'){ 
      prt.data('playPause','play') 
      play(); 
      playPause.html('||').addClass('paused'); 
     } 
     else { 
      holdItem(); 
      resetClock(); 
      prt.data('playPause','pause') 
      playPause.html('&#x25BA;').removeClass('paused'); 
     } 
    } 

    function holdItem(){ 
     clearTimeout(prt.data('timer')); 
    } 

    $.isFunction(options.setup) && options.setup.call(this); 
}); 

} 

謝謝!

回答

0

驗證訪問之前,該變量maxHright:

if (prt.data("options").maxHeight != null) 
{ 
    // access the variable 
} 
+0

所以我需要驗證我的所有變量的選擇? mH,autoStart和s(速度)。 – JemyDev

+0

我有'速度'相同的問題,但我增加了這一行︰ if((prt.data(「options」)。speed || options.speed)!= null){ max = itms.length; itms = prt.find(options.itms); s =(prt.data(「options」)。speed || options.speed)* 1000; t = s/360; (prt.data(「options」)。autoStart || options.autoStart!= null){ } autoStart = prt.data(「options」)。autoStart || options.autoStart; } if(prt.data(「options」).maxHeight!= null){ mH = prt.data(「options」).maxHeight; } – JemyDev

+0

您可以爲容器對象編寫一個驗證例程:options或prt.data(「options」),您可以隨時使用它。 – CloudyMarble