/**
* @classdescription 妯℃嫙marquee锛屾棤闂存柇婊氬姩鍐呭
* @author aken li(www.kxbd.com)
* @date 2009-07-17
* @dom
*
* @css
* #marquee {width:200px;height:50px;overflow:hidden;}
* @usage
* $('#marquee').kxbdmarquee(options);
* @options
* delay:绛夊緟澶氫箙鍚庢粴鍔紝鍗曚綅绉?
* isequal:true,//鎵€鏈夋粴鍔ㄧ殑鍏冪礌闀垮鏄惁鐩哥瓑,true,false
* loop: 0,//寰幆婊氬姩娆℃暟锛?鏃舵棤闄?
* direction: 'left',//婊氬姩鏂瑰悜锛?left','right','up','down'
* scrollamount:1,//姝ラ暱
* scrolldelay:20//鏃堕暱
* controlbtn:{left:'#gol',right:'#gor'},//鎺у埗鍔犻€熸粴鍔ㄧ殑鎸夐挳id锛屾湁鍥涗釜灞炴€eft,right,up,down鍒嗗埆瀵瑰簲鍥涗釜鏂瑰悜
* newamount:4,//鍔犻€熸粴鍔ㄧ殑姝ラ暱
* eventa:'mouseenter',//榧犳爣浜嬩欢锛屽姞閫?
* eventb:'mouseleave'//榧犳爣浜嬩欢锛屽師閫?
*/
(function($){
$.fn.kxbdmarquee = function(options){
var opts = $.extend({},$.fn.kxbdmarquee.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;//婊氬姩绫诲瀷锛?宸﹀彸锛?涓婁笅
//闃叉婊氬姩瀛愬厓绱犳瘮婊氬姩鍏冪礌瀹借€屽彇涓嶅埌瀹為檯婊氬姩瀛愬厓绱犲搴?
$element.css(_type?'width':'height',10000);
//鑾峰彇婊氬姩鍏冪礌鐨勫昂瀵?
if (opts.isequal) {
scrollsize = $kids[_type?'outerwidth':'outerheight']() * $kids.length;
}else{
$kids.each(function(){
scrollsize += $(this)[_type?'outerwidth':'outerheight']();
});
}
//婊氬姩鍏冪礌鎬诲昂瀵稿皬浜庡鍣ㄥ昂瀵革紝涓嶆粴鍔?
if (scrollsize<(_type?scrollw:scrollh)) return;
//鍏嬮殕婊氬姩瀛愬厓绱犲皢鍏舵彃鍏ュ埌婊氬姩鍏冪礌鍚庯紝骞惰瀹氭粴鍔ㄥ厓绱犲搴?
$element.append($kids.clone()).css(_type?'width':'height',scrollsize*2);
var nummoved = 0;
function scrollfunc(){
var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollleft':'scrolltop';
if (opts.loop > 0) {
nummoved+=opts.scrollamount;
if(nummoved>scrollsize*opts.loop){
_scrollobj[_dir] = 0;
return clearinterval(moveid);
}
}
if(opts.direction == 'left' || opts.direction == 'up'){
var newpos = _scrollobj[_dir] + opts.scrollamount;
if(newpos>=scrollsize){
newpos -= scrollsize;
}
_scrollobj[_dir] = newpos;
}else{
var newpos = _scrollobj[_dir] - opts.scrollamount;
if(newpos<=0){
newpos += scrollsize;
}
_scrollobj[_dir] = newpos;
}
};
//婊氬姩寮€濮?
var moveid;
if(opts.delay>0){
settimeout(function(){
moveid = setinterval(scrollfunc, opts.scrolldelay);
},opts.delay*1000);
}else{
moveid = setinterval(scrollfunc, opts.scrolldelay);
}
//榧犳爣鍒掕繃鍋滄婊氬姩
$marquee.hover(
function(){
clearinterval(moveid);
},
function(){
clearinterval(moveid);
moveid = setinterval(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;
});
});
}
});
};
$.fn.kxbdmarquee.defaults = {
delay:0,
isequal:true,//鎵€鏈夋粴鍔ㄧ殑鍏冪礌闀垮鏄惁鐩哥瓑,true,false
loop: 0,//寰幆婊氬姩娆℃暟锛?鏃舵棤闄?
direction: 'left',//婊氬姩鏂瑰悜锛?left','right','up','down'
scrollamount:1,//姝ラ暱
scrolldelay:30,//鏃堕暱
newamount:3,//鍔犻€熸粴鍔ㄧ殑姝ラ暱
eventa:'mousedown',//榧犳爣浜嬩欢锛屽姞閫?
eventb:'mouseup'//榧犳爣浜嬩欢锛屽師閫?
};
$.fn.kxbdmarquee.setdefaults = function(settings) {
$.extend( $.fn.kxbdmarquee.defaults, settings );
};
})(jquery);