滑块bug javascript

时间:2013-01-11 11:52:07

标签: javascript jquery slider

我的控制台上有错误=>未捕获的TypeError:无法读取null的属性“maxHeight”。

我认为这是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);
});

}

谢谢!

1 个答案:

答案 0 :(得分:0)

在访问变量maxHright之前验证它:

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