滑块错误javascript

滑块错误javascript,javascript,jquery,slider,Javascript,Jquery,Slider,我的控制台上有一个错误=>UncaughtTypeError:无法读取null的属性“maxHeight” 我认为jQuery版本有问题,但如果你能帮我的话,我不知道如何解决这个问题。jQuery版本:1.4.2 代码如下: $.fn.slider = function(options, callback) { if ($.isFunction(options)) { callback = options; options = null; } options = $.exten

我的控制台上有一个错误=>UncaughtTypeError:无法读取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);
});

}
$.fn.slider=函数(选项,回调){
如果($.isFunction(选项)){
回调=选项;
选项=空;
}
选项=$.extend($.fn.slider.defaults,options);
返回此值。每个(函数(){
var prt=$(此),itms=prt.find(options.itms),w=prt.WITH(),max=itms.length,mH=prt.data(“options”).MAXHEIGH,p=0,autoStart=prt.data(“options”).autoStart | | options.autoStart,s=(prt.data(“options”).speed | | options.speed)*1000,t=s/360;
如果(max0)?c.索引():0;
var迭代器=“”;
itms.each(函数(){
var i=$(this.index();
迭代器+='&bull;';
});
$('laquo;'+迭代器+'raquo;&x25BA;')。附录(prt);
var iteratorBtns=prt.find('.iterators按钮');
var playPause=prt.find('.playPause');
var clock=prt.find('.clock');
iteratorBtns.eq(cId.addClass('on');
prt.find('.next').bind('click',function(){
hold();
移位('fwd','static');
});
prt.find('.prev').bind('click',function()){
hold();
shiftItem('back','static');
});
iteratorBtns.bind('click',function(){
hold();
shiftItem('unknown','static',$(this.index());
});
playpaise.bind('单击',函数()){
切换();
});
prt.bind('mouseenter',function(){
hold();
});
prt.bind('mouseleave',function(){
if(autoStart==true)
切换();
});
//先进的
eq(cId).fadeTo(0,0).animate({'left':p,'opacity':1},0,function(){
$(this.css({'filter':'none'})
});
prt.数据(“状态”、“空闲”);
prt.data(‘播放暂停’、‘暂停’);
if(autoStart==true)
切换();
功能移位(方向、类型、n){
holdItem();
var w=prt.width();
如果(prt.data('status')=='moving')返回;
prt.数据(“状态”、“移动”);
如果(方向==“未知”){
如果(n==cId){
prt.数据(“状态”、“空闲”);
如果(键入!='static')play();
返回;
}
方向=(n>cId)?“前进”:“后退”;
}
其他的
变量n=(方向='fwd')?(cId+1==max)?0:cId+1:(cId-1<0)?max-1:cId-1;
变量sS=(方向='fwd')?w+(2*parseInt(p,10)):-w-(2*parseInt(p,10));
eq(cId).animate({'opacity':0},700,function(){});
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;
如果(类型!=“静态”){
play();
}
prt.数据(“状态”、“空闲”);
});
}
var clockMax=572,clockWidth=26,clockSlices=22,clockSpeed=s/clockSlices,currentPosition=0;
函数setClock(){
重置时钟();
clock.data('timer',setTimeout(函数()){
shiftlock();
},钟速);
}
函数shiftlock(){
currentPosition-=时钟宽度;
css({'background-position':currentPosition+'px0'});
clock.data('timer',setTimeout(函数()){
shiftlock();
},钟速);
}
函数resetClock(){
clearTimeout(时钟数据('timer'));
currentPosition=0;
css({'background-position':'0'});
}
函数保持(){
holdItem();
prt.data('playPause','pause')
html(►;).removeClass('paused');
重置时钟();
}
函数播放(){
如果(prt.data('playPause')=='play'){
prt.data('timer',setTimeout(函数()){
移位('fwd','dynamic');
},s));
设置时钟();
}
}
函数切换(){
变量状态=(prt.data('playPause')=='pause')?'play':'pause';
如果(状态=='play'){
prt.data(‘播放暂停’、‘播放’)
play();
playPause.html(“| |”).addClass('paused');
}
否则{
holdItem();
重置时钟();
prt.data('playPause','pause')
html(►;).removeClass('paused');
}
}
函数holdItem(){
clearTimeout(prt.data(‘计时器’);
}
$.isFunction(options.setup)和&options.setup.call(this);
});
}

谢谢

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

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

所以我需要验证我所有的变量选项?mH,autoStart和s(表示速度)。我对“速度”有相同的问题,但我添加了以下行: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;}if(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;}您可以为容器对象编写验证路由:options或prt.data(“options”),每次都可以使用。您有一个对象,比如说options,您需要验证te height、speed等,因此您可以编写一个例程来接收options类型的对象并验证其中的所有变量,而不是在程序中重复执行这些操作,一旦缺少值,就可以考虑SETIGGN默认值。