/**
* @classdescription 瓒呯骇marquee锛屽彲鍋氬浘鐗囧鑸紝鍥剧墖杞崲
* @author aken li(www.kxbd.com)
* @date 2009-07-27
* @dependence jquery 1.3.2
* @dom
*
* @css
* #marquee {width:200px;height:50px;overflow:hidden;}
* @usage
* $('#marquee').kxbdsupermarquee(options);
* @options
* distance:200,//涓€娆℃粴鍔ㄧ殑璺濈
* duration:20,//缂撳姩鏁堟灉锛屽崟娆$щ鍔ㄦ椂闂达紝瓒婂皬閫熷害瓒婂揩锛屼负0鏃舵棤缂撳姩鏁堟灉
* time:5,//鍋滈】鏃堕棿锛屽崟浣嶄负绉?
* direction: 'left',//婊氬姩鏂瑰悜锛?left','right','up','down'
* scrollamount:1,//姝ラ暱
* scrolldelay:20//鏃堕暱锛屽崟浣嶄负姣
* isequal:true,//鎵€鏈夋粴鍔ㄧ殑鍏冪礌闀垮鏄惁鐩哥瓑,true,false
* loop: 0,//寰幆婊氬姩娆℃暟锛?鏃舵棤闄?
* btngo:{left:'#gol',right:'#gor'},//鎺у埗鏂瑰悜鐨勬寜閽甀d锛屾湁鍥涗釜灞炴€eft,right,up,down鍒嗗埆瀵瑰簲鍥涗釜鏂瑰悜
* eventgo:'click',//榧犳爣浜嬩欢
* controlbtn:{left:'#gol',right:'#gor'},//鎺у埗鍔犻€熸粴鍔ㄧ殑鎸夐挳id锛屾湁鍥涗釜灞炴€eft,right,up,down鍒嗗埆瀵瑰簲鍥涗釜鏂瑰悜
* newamount:4,//鍔犻€熸粴鍔ㄧ殑姝ラ暱
* eventa:'mouseenter',//榧犳爣浜嬩欢锛屽姞閫?
* eventb:'mouseleave',//榧犳爣浜嬩欢锛屽師閫?
* navid:'#marqueenav', //瀵艰埅瀹瑰櫒id锛屽鑸狣om:- 1
- 2
,瀵艰埅css:.navon
* eventnav:'click' //瀵艰埅浜嬩欢
*/
(function($){
$.fn.kxbdsupermarquee = function(options){
var opts = $.extend({},$.fn.kxbdsupermarquee.defaults, options);
return this.each(function(){
var $marquee = $(this);//婊氬姩鍏冪礌瀹瑰櫒
var _scrollobj = $marquee.get(0);//婊氬姩鍏冪礌瀹瑰櫒dom
var scrollw = $marquee.width();//婊氬姩鍏冪礌瀹瑰櫒鐨勫搴?
var scrollh = $marquee.height();//婊氬姩鍏冪礌瀹瑰櫒鐨勯珮搴?
var $element = $marquee.children(); //婊氬姩鍏冪礌
var $kids = $element.children();//婊氬姩瀛愬厓绱?
var scrollsize=0;//婊氬姩鍏冪礌灏哄
var _type = (opts.direction == 'left' || opts.direction == 'right') ? 1:0;//婊氬姩绫诲瀷锛?宸﹀彸锛?涓婁笅
var scrollid, rollid, ismove, marqueeid;
var t,b,c,d,e; //婊氬姩鍔ㄧ敾鐨勫弬鏁?t:褰撳墠鏃堕棿锛宐:寮€濮嬬殑浣嶇疆锛宑:鏀瑰彉鐨勪綅缃紝d:鎸佺画鐨勬椂闂达紝e:缁撴潫鐨勪綅缃?
var _size, _len; //瀛愬厓绱犵殑灏哄涓庝釜鏁?
var $nav,$navbtns;
var arrpos = [];
var numview = 0; //褰撳墠鎵€鐪嬪瓙鍏冪礌
var numroll=0; //杞崲鐨勬鏁?
var nummoved = 0;//宸茬粡绉诲姩鐨勮窛绂?
//闃叉婊氬姩瀛愬厓绱犳瘮婊氬姩鍏冪礌瀹借€屽彇涓嶅埌瀹為檯婊氬姩瀛愬厓绱犲搴?
$element.css(_type?'width':'height',10000);
//鑾峰彇婊氬姩鍏冪礌鐨勫昂瀵?
var navhtml = '';
if (opts.isequal) {
_size = $kids[_type?'outerwidth':'outerheight']();
_len = $kids.length;
scrollsize = _size * _len;
for(var i=0;i<_len;i++){
arrpos.push(i*_size);
navhtml += '- '+ (i+1) +'
';
}
}else{
$kids.each(function(i){
arrpos.push(scrollsize);
scrollsize += $(this)[_type?'outerwidth':'outerheight']();
navhtml += '- '+ (i+1) +'
';
});
}
navhtml += '
';
//婊氬姩鍏冪礌鎬诲昂瀵稿皬浜庡鍣ㄥ昂瀵革紝涓嶆粴鍔?
if (scrollsize<(_type?scrollw:scrollh)) return;
//鍏嬮殕婊氬姩瀛愬厓绱犲皢鍏舵彃鍏ュ埌婊氬姩鍏冪礌鍚庯紝骞惰瀹氭粴鍔ㄥ厓绱犲搴?
$element.append($kids.clone()).css(_type?'width':'height',scrollsize*2);
//杞崲瀵艰埅
if (opts.navid) {
$nav = $(opts.navid).append(navhtml).hover( stop, start );
$navbtns = $('li', $nav);
$navbtns.each(function(i){
$(this).bind(opts.eventnav,function(){
if(ismove) return;
if(numview==i) return;
rollfunc(arrpos[i]);
$navbtns.eq(numview).removeclass('navon');
numview = i;
$(this).addclass('navon');
});
});
$navbtns.eq(numview).addclass('navon');
}
//璁惧畾鍒濆浣嶇疆
if (opts.direction == 'right' || opts.direction == 'down') {
_scrollobj[_type?'scrollleft':'scrolltop'] = scrollsize;
}else{
_scrollobj[_type?'scrollleft':'scrolltop'] = 0;
}
if(opts.ismarquee){
//婊氬姩寮€濮?
//marqueeid = setinterval(scrollfunc, opts.scrolldelay);
marqueeid = settimeout(scrollfunc, opts.scrolldelay);
//榧犳爣鍒掕繃鍋滄婊氬姩
$marquee.hover(
function(){
clearinterval(marqueeid);
},
function(){
//marqueeid = setinterval(scrollfunc, opts.scrolldelay);
clearinterval(marqueeid);
marqueeid = settimeout(scrollfunc, opts.scrolldelay);
}
);
//鎺у埗鍔犻€熻繍鍔?
if(opts.controlbtn){
$.each(opts.controlbtn, function(i,val){
$(val).bind(opts.eventa,function(){
opts.direction = i;
opts.oldamount = opts.scrollamount;
opts.scrollamount = opts.newamount;
}).bind(opts.eventb,function(){
opts.scrollamount = opts.oldamount;
});
});
}
}else{
if(opts.isauto){
//杞崲寮€濮?
start();
//榧犳爣鍒掕繃鍋滄杞崲
$marquee.hover( stop, start );
}
//鎺у埗鍓嶅悗璧?
if(opts.btngo){
$.each(opts.btngo, function(i,val){
$(val).bind(opts.eventgo,function(){
if(ismove == true) return;
opts.direction = i;
rollfunc();
if (opts.isauto) {
stop();
start();
}
});
});
}
}
function scrollfunc(){
var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollleft':'scrolltop';
if(opts.ismarquee){
if (opts.loop > 0) {
nummoved+=opts.scrollamount;
if(nummoved>scrollsize*opts.loop){
_scrollobj[_dir] = 0;
return clearinterval(marqueeid);
}
}
var newpos = _scrollobj[_dir]+(opts.direction == 'left' || opts.direction == 'up'?1:-1)*opts.scrollamount;
}else{
if(opts.duration){
if(t++=scrollsize){
newpos-=scrollsize;
}
}else{
if(newpos<=0){
newpos+=scrollsize;
}
}
_scrollobj[_dir] = newpos;
if(opts.ismarquee){
marqueeid = settimeout(scrollfunc, opts.scrolldelay);
}else if(t=_len){
numview = 0;
}else if(numview<0){
numview = _len-1;
}
$navbtns.eq(numview).addclass('navon');
numroll = numview;
}
var _temp = numroll<0?scrollsize:0;
t=0;
b=_scrollobj[_dir];
//c=(ppos != undefined)?ppos:_neg*opts.distance;
e=(ppos != undefined)?ppos:_temp+(opts.distance*numroll)%scrollsize;
if(_neg==1){
if(e>b){
c = e-b;
}else{
c = e+scrollsize -b;
}
}else{
if(e>b){
c =e-scrollsize-b;
}else{
c = e-b;
}
}
d=opts.duration;
//scrollid = setinterval(scrollfunc, opts.scrolldelay);
if(scrollid) cleartimeout(scrollid);
scrollid = settimeout(scrollfunc, opts.scrolldelay);
}
function start(){
rollid = setinterval(function(){
rollfunc();
}, opts.time*1000);
}
function stop(){
clearinterval(rollid);
}
function easeoutquad(t,b,c,d){
return -c *(t/=d)*(t-2) + b;
}
function easeoutquint(t,b,c,d){
return c*((t=t/d-1)*t*t*t*t + 1) + b;
}
});
};
$.fn.kxbdsupermarquee.defaults = {
ismarquee:false,//鏄惁涓篗arquee
isequal:true,//鎵€鏈夋粴鍔ㄧ殑鍏冪礌闀垮鏄惁鐩哥瓑,true,false
loop: 0,//寰幆婊氬姩娆℃暟锛?鏃舵棤闄?
newamount:3,//鍔犻€熸粴鍔ㄧ殑姝ラ暱
eventa:'mousedown',//榧犳爣浜嬩欢锛屽姞閫?
eventb:'mouseup',//榧犳爣浜嬩欢锛屽師閫?
isauto:true,//鏄惁鑷姩杞崲
time:5,//鍋滈】鏃堕棿锛屽崟浣嶄负绉?
duration:50,//缂撳姩鏁堟灉锛屽崟娆$щ鍔ㄦ椂闂达紝瓒婂皬閫熷害瓒婂揩锛屼负0鏃舵棤缂撳姩鏁堟灉
eventgo:'click', //榧犳爣浜嬩欢锛屽悜鍓嶅悜鍚庤蛋
direction: 'left',//婊氬姩鏂瑰悜锛?left','right','up','down'
scrollamount:1,//姝ラ暱
scrolldelay:10,//鏃堕暱
eventnav:'click'//瀵艰埅浜嬩欢
};
$.fn.kxbdsupermarquee.setdefaults = function(settings) {
$.extend( $.fn.kxbdsupermarquee.defaults, settings );
};
})(jquery);