0

所以我是插件开发的新手,我一直在做一个图像轮播,想把它变成一个插件。我以为我已经弄清楚了,但似乎无法正确解决。init 函数不会调用。这是我的插件代码:

// Plugin
(function( $ ){

  $.fn.customSlider = function(options){

    var config = $.extend({ // TODO: add all configuration options needed
      'slideCount': null,
      'delayTime': 1000
    }, options);

    var slideCount  = this.find('div.slide').length,
        rotate      = function(i){ // TODO: make name more descriptive
            setInterval(function() {
                if (i < slideCount -1) {
                    i++;
                } else {
                    i=0;
                }
                slideRotate(i);
            },  delayTime);
        }, 

        slideRotate = function(i){
            var asdf = null;
            $('.slider').children().fadeOut(500);
            $($('.slider div').get(i)).fadeIn(500); // TODO: externalize $('.slider div').get(i) into variable
        },

        gotoSlide = function(i){
            customSlider.rotate(i-1);
        },

        init = function(){
            console.log('init');
            this.find('div:first').show();
            customSlider.rotate(0);
        }, 

        clickzor = function() { // TODO: make selector more specific  
            clearInterval(intervalID); 
            intervalID = null; 
            var num = parseInt($(this).text(),'') - 1; 
            slideRotate(num); 
            gotoSlide(num+1);
        };

        $('a').click(clickzor);

    };

    customSlider.init();

    // init

})( jQuery );

$(function(){
    $('#headSlider').customSlider();
});

这是我的工作非插件代码:

$(function() {
    $('#headSlider div.slide:first').addClass('active').show();
    var slideCount = $('.slider div.slide').length;
    var delayTime = 6000;
    var intervalID;



    function slideRotate(i) {
        $('.slider').children().fadeOut(500);
        $($('.slider div').get(i)).fadeIn(500);     
    }

    function rotate(i) 
    {            
        intervalID = setInterval(function() {
            if(i < slideCount -1)
            {
                i++;
            }
            else
            {
                i=0;
            }
            slideRotate(i);
        }, delayTime);
    }

    function gotoSlide(i)
    {        
        rotate(i-1);
    }

     $('a').click(function() 
    {     
        clearInterval(intervalID); 
        intervalID = null; 
        var num = parseInt($(this).text(),'') - 1; 
        slideRotate(num); 
        gotoSlide(num+1);
    });


    rotate(0);
});

我只是不知道我做错了什么。帮助!

4

1 回答 1

0

我相信问题是你应该init()在你的扩展定义中调用。

$.fn.slider = function(options){
    ...definitions...
    init();
}
于 2012-07-04T19:48:50.360 回答