$.fn.extend({ /*---------------------------- 多行文字滚动代码 v1.1 @author:wlf @qq: 4461329 @addtime: 2012-12-17 @lastedit: 2013-5-31 -----------------------------*/ Scroll: function(option, callback) { //参数初始化 var opt = { line:1, speed:500, timer:2800, upbtn:"",//向上按钮 downbtn:"",//向下按钮 type:"",//up | left leftbtn:"", rightbtn:"", nownumobj:"", totalnumobj:"", autoscroll:true }; $.extend(opt,option); var _btnUp = $(opt.upbtn); var _btnDown = $(opt.downbtn); var _btnLeft = $(opt.leftbtn); var _btnRight = $(opt.rightbtn); var _nownumObj = $(opt.nownumobj); var _totalnumObj = $(opt.totalnumobj); var timerID; var nowLi=1;//当前Li var _this = this.eq(0).find("ul:first"); var lineH = _this.find("li:first").height(),//获取行高 lineW = _this.find("li:first").width(),//获取行宽 totalLi = _this.find("li").length, //总条数 //每次滚动的行数,默认为一屏,即父容器高度 line = opt.line ? parseInt(opt.line, 10) : parseInt((opt.type=="up"?this.height() / lineH:this.width() / lineW), 10), _speed = (opt.speed ? parseInt(opt.speed, 10) : 500);//卷动速度,数值越大,速度越慢(毫秒) _timer = opt.timer; //?parseInt(opt.timer,10):3000; //滚动的时间间隔(毫秒) if (line == 0) line = 1; var upHeight = 0 - line * lineH; var LeftWidth = 0 - line * lineW; //滚动函数 var scrollUp = function() { _btnUp.unbind("click", scrollUp);//取消向上按钮的函数绑定 _this.animate({ marginTop: upHeight }, _speed, function() { for (i = 1; i <= line; i++) { _this.find("li:first").appendTo(_this); } _this.css({ marginTop: 0 }); _btnUp.bind("click", scrollUp);//绑定向上按钮的点击事件 }); } //向下翻页函数 var scrollDown = function() { _btnDown.unbind("click", scrollDown); for (i = 1; i <= line; i++) { _this.find("li:last").show().prependTo(_this); } _this.css({ marginTop: upHeight }); _this.animate({ marginTop: 0 }, _speed, function() { _btnDown.bind("click", scrollDown); }); } //向左滚动 var scrollLeft = function(){ $("*").stop(); _btnLeft.unbind("click", scrollLeft);//取消向上按钮的函数绑定 _this.animate({ "marginLeft": LeftWidth }, _speed, function() { SetNum(); for (i = 1; i <= line; i++) { _this.find("li:first").appendTo(_this); } _this.css({ "marginLeft": 0 }); _btnLeft.bind("click", scrollLeft);//绑定向上按钮的点击事件 }); }; //向右滚动 var scrollRight = function() { $("*").stop(); _btnDown.unbind("click", scrollRight); for (i = 1; i <= line; i++) { _this.find("li:last").show().prependTo(_this); } _this.css({ "marginLeft": LeftWidth }); _this.animate({ "marginLeft": 0 }, _speed, function() { SetNum(); _btnDown.bind("click", scrollRight); }); } var SetNum = function(){ //数值显示 if(nowLi>totalLi)nowLi=1; _nownumObj.html(nowLi); _totalnumObj.html(totalLi); nowLi++; } //自动播放 var autoPlay = function() { if (_timer) { if(opt.type=="up"){ timerID = window.setInterval(scrollUp, _timer); }else{ timerID = window.setInterval(scrollLeft, _timer); } }; }; var autoStop = function() { if (_timer) window.clearInterval(timerID); }; //鼠标事件绑定 //_this.hover(autoStop, autoPlay).mouseout(); _btnUp.css("cursor", "pointer").click(scrollUp)//.hover(autoStop, autoPlay);//向上向下鼠标事件绑定 _btnDown.css("cursor", "pointer").click(scrollDown)//.hover(autoStop, autoPlay); _btnLeft.css("cursor", "pointer").click(scrollLeft)//.hover(autoStop, autoPlay); _btnRight.css("cursor", "pointer").click(scrollRight)//.hover(autoStop, autoPlay); SetNum(); if (_timer && opt.autoscroll) autoPlay(); }, ScrollEasy:function(type,demo1,demo2,speed){ var timerID,demo = this.eq(0),demo1 = $(demo1),demo2 = $(demo2); demo2.html(demo1.html()); //左 var MarqueeLeft = function(){ if(demo.scrollLeft()>=demo1.width()) demo.scrollLeft(0); else{ demo.scrollLeft(demo.scrollLeft()+1); } } //右 var MarqueeRight = function (){ if(demo.scrollLeft()==0){ demo.scrollLeft(demo1.width()); }else{ demo.scrollLeft(demo.scrollLeft()-2); } } //自动播放 var autoPlay = function() { if (speed) { if (type=="left"){ timerID = window.setInterval(MarqueeLeft,speed); }else{ timerID = window.setInterval(MarqueeRight,speed); } } }; var autoStop = function() { if (speed) window.clearInterval(timerID); }; //鼠标事件绑定 demo.hover(autoStop, autoPlay).mouseout(); } });