$.fn.barrage = function(opt){ var _self = $(this); var opts = { //默认参数 data : [], //数据列表 row : 5, //显示行数 time : 2000, //间隔时间 gap : 20, //每一个的间隙 position : 'fixed', //绝对定位 direction : 'bottom right', //方向 ismoseoverclose : true, //悬浮是否停止 height : 30, } var settings = $.extend({},opts,opt); //合并参数 var M = {},Obj = {}; Obj.data = settings.data; M.vertical = settings.direction.split(/\s+/)[0]; //纵向 M.horizontal = settings.direction.split(/\s+/)[1]; //横向 M.bgColors = ['#555555','#888888','#666666','#aaaaaa','#bbbbbb','#cccccc','#dddddd','#eeeeee']; //随机背景色数组 Obj.arrEle = []; //预计存储dom集合数组 M.barrageBox = $('
'); //存所有弹幕的盒子 M.timer = null; var createView = function(){ var randomIndex = Math.floor(Math.random() * M.bgColors.length); var ele = $(''+Obj.data[0].text+''); var str = Obj.data.shift(); if(M.vertical == 'top'){ ele.animate({ 'opacity' : 1, 'margin-top' : settings.gap, 'height' : settings.height, 'line-height' : settings.height+'px', },1000) M.barrageBox.prepend(ele); }else{ ele.animate({ 'opacity' : 1, 'margin-bottom' : settings.gap, 'height' : settings.height, 'line-height' : settings.height+'px' },1000) M.barrageBox.append(ele); } Obj.data.push(str); if(M.barrageBox.children().length > settings.row){ M.barrageBox.children().eq(0).animate({ 'opacity' : 0, },300,function(){ $(this).css({ 'margin' : 0, }).remove(); }) } } M.mouseClose = function(){ settings.ismoseoverclose && (function(){ M.barrageBox.mouseover(function(){ clearInterval(M.timer); M.timer = null; }).mouseout(function(){ M.timer = setInterval(function(){ //循环 createView(); },settings.time) }) })() } Obj.close = function(){ M.barrageBox.remove(); clearInterval(M.timer); M.timer = null; } Obj.start = function(){ if(M.timer) return; _self.append(M.barrageBox); //把弹幕盒子放到页面中 createView(); //创建试图并开始动画 M.timer = setInterval(function(){ //循环 createView(); },settings.time) M.mouseClose(); } return Obj; } var data = [ {href : 'javascript:;',text : '办理许可证速度非常快,我问了好几家公司了,好助手非常不错'}, {href : 'javascript:;',text : '别的公司问问题都拖拖拉拉的,好助手的服务杠杠的,有问必答,凌晨一点还在'}, {href : 'javascript:;',text : '非常可以的,老熟人了,介绍好几个朋友啦,希望可以一直长久合作下去'}, {href : 'javascript:;',text : '收费非常明确的,没有乱收费,有专门的老师为我解答问题,赞!!'}, {href : 'javascript:;',text : '全程非常省心,不知不觉就做好了,需要什么都提前说出来,非常专业,值得信赖!'} ]