Javascript 如何将自定义jQuery插件应用于多个元素?

Javascript 如何将自定义jQuery插件应用于多个元素?,javascript,jquery,plugins,Javascript,Jquery,Plugins,我制作了一个jQuery插件,当应用于单个元素时,它可以很好地工作。 当我将其应用于另一个元素时,前面的元素将停止其应有的行为。 这样,只有最后应用的元素才起作用。 当开发插件来创建所有元素的前提下,我会考虑什么?p> 这就是我制作插件的方式: (function($) { $.fn.scrollabix = function(params) { return this.each(function(){ // my code

我制作了一个jQuery插件,当应用于单个元素时,它可以很好地工作。
当我将其应用于另一个元素时,前面的元素将停止其应有的行为。
这样,只有最后应用的元素才起作用。
当开发插件来创建所有元素的前提下,我会考虑什么?p> 这就是我制作插件的方式:

 (function($) {  

      $.fn.scrollabix = function(params) {

        return this.each(function(){
               // my code
            });           

      };  

 })(jQuery);
这是全部代码:

(function($) {  
        // jQuery plugin definition  
        $.fn.scrollabix = function(params) {

            return this.each(function(){

                // OPTIONS/Config
                    $defaultOptions = 
                                        {
                                            direction: 'vertical'
                                        };
                    $.extend($defaultOptions, params);

                // LOCAL PARAMETERS
                    $self = $(this);
                    $scrolled = $("> div", this);                   

                // GET INFO
                    $selfHeight  = parseInt($self.css("height"));
                    $scrolledHeight = parseInt($('> div', $self).css("height"));

                    $selfWidth  = parseInt($self.css("width"));
                    $scrolledWidth = parseInt($('> div', $self).css("width"));

                    $leftOutside = ($scrolledHeight) - parseInt($selfHeight) /*+ 50*/;  // user for Vertivcal only


                // PLUGIN ACTIONS
                    actions = {
                        continous : 0,
                        slideUp : function(q)
                        {
                            if($leftOutside > 0)
                            {
                                slideBy =(120 * 20) / q;

                                currTop = parseInt($scrolled.css("top"));
                                absCurrTop = Math.abs(currTop);

                                if(absCurrTop + slideBy > ($leftOutside))
                                {
                                    $scrolled.stop().animate({top : '-' + $leftOutside + 'px'}, 250);
                                }
                                else
                                {
                                    if(absCurrTop < ($leftOutside))
                                    {
                                        $scrolled.stop().animate({top : '-=' + slideBy}, 250, 'linear', function(){
                                            if(actions.continous == 1)
                                            {
                                                actions.slideUp(q);
                                            }
                                        });
                                    }
                                }                           
                            }
                        },
                        slideDown : function(q)
                        {
                            if($leftOutside > 0)
                            {
                                slideBy =(120 * 20) / q;

                                currTop = parseInt($scrolled.css("top"));
                                absCurrTop = Math.abs(currTop);

                                if(absCurrTop - slideBy < 0)
                                {
                                    slideBy = absCurrTop;
                                }
                                if(currTop < 0)
                                {
                                    $scrolled.stop().animate({top : '+=' + slideBy}, 250, 'linear', function(){
                                        if(actions.continous == 1)
                                        {
                                            actions.slideDown(q);
                                        }
                                    });
                                }
                            }
                        },
                        slideLeft : function(q)
                        {
                            if($leftOutside > 0)
                            {
                                consolex('slideLeft');
                            }                           
                        },
                        slideRight : function(q)
                        {
                            if($leftOutside > 0)
                            {
                                consolex('slideRight');
                            }
                        }
                    }

                // CREATE INFRASTRUCTURE
                    $self.css({
                        overflow : 'hidden',
                        position : 'relative'
                    });
                    $scrolled.css({
                        position : 'absolute',
                        top : '0px',
                        left : '0px'
                    });

                    if($defaultOptions.direction == 'vertical')
                    {
                        $self.mousemove(function(e){
                            var x = parseInt(e.pageX - this.offsetLeft);
                            var y = parseInt(e.pageY - this.offsetTop);

                            if(y > ($selfHeight - 120) && y < $selfHeight)
                            {
                                actions.continous = 1;
                                actions.slideUp($selfHeight - y);
                            }
                            else if(y < 120 && y >= 1)
                            {
                                actions.continous = 1;
                                actions.slideDown(y);           
                            }
                            else
                            {
                                actions.continous = 0;
                            }
                        }).mouseout(function(){
                            actions.continous = 0;
                        });
                    }
                    else if ($defaultOptions.direction == 'horizontal')
                    {

                        $leftOutside = $scrolledWidth - $selfWidth;                     

                        $self.mousemove(function(e){
                            $scrolledWidth = parseInt($scrolled.css('width'));

                            var x = parseInt(e.pageX - this.offsetLeft);
                            $selfWidth = parseInt($self.css("width"));
                            $leftOutside = ($scrolledWidth) - parseInt($selfWidth);
                            consolex($leftOutside);

                            if(x < 300 && x >= 1)
                            {
                                actions.continous = 1;
                                actions.slideRight(x);                              
                            }
                            else if( x > $selfWidth - 300)
                            {
                                actions.continous = 1;
                                actions.slideLeft(x);                               
                            }

                        }).mouseout(function(){
                            actions.continous = 0;
                        });
                    }
                //return this;
            });           
        };  
    })(jQuery);
(函数($){
//jQuery插件定义
$.fn.scrollabix=函数(参数){
返回此值。每个(函数(){
//选项/配置
$defaultOptions=
{
方向:“垂直”
};
$.extend($defaultOptions,params);
//局部参数
$self=$(这个);
$scrolled=$(“>div”,此);
//获取信息
$selfHeight=parseInt($self.css(“height”);
$scrolledHeight=parseInt($('>div',$self).css(“高度”);
$selfWidth=parseInt($self.css(“width”);
$scrolledWidth=parseInt($('>div',$self).css(“宽度”);
$leftOutside=($scrolledHeight)-parseInt($selfHeight)/*+50*//;//仅限Vertivcal用户
//插件操作
行动={
连续:0,
幻灯片:功能(q)
{
如果($leftOutside>0)
{
slideBy=(120*20)/q;
currTop=parseInt($scrolled.css(“top”));
absCurrTop=Math.abs(currTop);
如果(absCurrTop+slideBy>($leftOutside))
{
$scrolled.stop().animate({top:'-'+$leftOutside+'px'},250);
}
其他的
{
如果(absCurrTop<($leftOutside))
{
$scrolled.stop().animate({top:'-='+slideBy},250,'linear',function(){
if(actions.continuous==1)
{
动作。slideUp(q);
}
});
}
}                           
}
},
向下滑动:函数(q)
{
如果($leftOutside>0)
{
slideBy=(120*20)/q;
currTop=parseInt($scrolled.css(“top”));
absCurrTop=Math.abs(currTop);
如果(absCurrTop-slideBy<0)
{
slideBy=absCurrTop;
}
if(currTop<0)
{
$scrolled.stop().animate({top:'+='+slideBy},250,'linear',function()){
if(actions.continuous==1)
{
动作。向下滑动(q);
}
});
}
}
},
slideLeft:功能(q)
{
如果($leftOutside>0)
{
consolex(“slideLeft”);
}                           
},
幻灯片灯光:功能(q)
{
如果($leftOutside>0)
{
consolex(“slideRight”);
}
}
}
//创建基础设施
$self.css({
溢出:“隐藏”,
位置:'相对'
});
$scrolled.css({
位置:'绝对',
顶部:“0px”,
左:“0px”
});
如果($defaultOptions.direction=='vertical')
{
$self.mousemove(函数(e){
var x=parseInt(e.pageX-this.offsetLeft);
var y=parseInt(e.pageY-this.offsetTop);
如果(y>($selfHeight-120)和&y<$selfHeight)
{
动作连续=1;
动作.slideUp($selfHeight-y);
}
如果(y<120&&y>=1),则为else
{
动作连续=1;
动作。向下滑动(y);
}
其他的
{
actions.continuous=0;
}
}).mouseout(函数(){
actions.continuous=0;
});
}
// A really lightweight plugin wrapper around the constructor, 
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
    return this.each(function () {
        if (!$.data(this, 'plugin_' + pluginName)) {
            $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
        }
    });
}