跑马灯组件

新年上班第一天,研究了下requirejs,其中一部分是对JQuery插件配置的API,即用shim属性,

require.config({
        shim:{
                "jquery.marquee":{
                        deps:["jquery"],
                        exports:"$.fn.marquee"
                }
        }
});

手懒没有找其它组件,现写了一个跑马灯的jQuery组件jquery.marquee.js,分享出来,供大家参考:

(function($){
        var old=$.fn.marquee;
        
        function Marquee(ele,options){
                this.ver='1.0';
                this.element=$(ele);
                this.options=$.extend({},$.fn.marquee.defaults, options);
                this.init();
        }
        
        Marquee.prototype={
                constructor:Marquee,
                init:function(){
                        var options=this.options,
                                $ele=this.element;
                        
                        $ele.text(options.text);
                        
                        setInterval(function(){
                                if(options.direction=="left"){
                                        var firstChar=$ele.text().charAt(0);
                                        $ele.text($ele.text().substring(1)+firstChar);
                                }else{
                                        var text=$ele.text(),
                                                l=text.length,
                                                lastChar=text.charAt(l-1);
                                        $ele.text(lastChar+text.substring(0,l-1));
                                }
                                
                        },options.timer);
                }
        };
        
        //jQuery插件
        $.fn.marquee=function(options){
                return this.each(function(){
                        var $this=$(this),
                                instance=$this.data('marquee');
                                
                        if(!instance){
                                instance=new Marquee(this,options);
                                $this.data('marquee',instance);
                        }else{
                                instance.init();
                        }
                });
        };
        
        $.fn.marquee.defaults={
                text:"",
                timer:100,
                direction:"left"
        };
        
        $.fn.marquee.noConflict=function(){
                $.fn.marquee=old;
                return this;
        };
        
})(jQuery);

requirejs使用方法:

require(["jquery.marquee"],function(marquee){
        $('#div1').marquee({
                text:"这里的文字滚动",
                timer:200,
                direction:"right"
        });
});

普通使用方法:

$('#div1').marquee({
                text:"这里的文字滚动",
                timer:200,
                direction:"right"
        });
这里有三个参数:text:是指滚动的文字,timer:滚动间隔,direction:滚动方向(left/right)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值