/**
* @classdescription 妯℃嫙marquee锛屾棤闂存柇婊氬姩鍐呭
* @author aken li(www.kxbd.com)
* @dom
*
* @css
* #marquee {width:200px;height:50px;overflow:hidden;}
* @usage
* $('#marquee').kxbdmarquee(options);
* @options
* isequal:true,//鎵€鏈夋粴鍔ㄧ殑鍏冪礌闀垮鏄惁鐩哥瓑,true,false
* loop: 0,//寰幆婊氬姩娆℃暟锛?鏃舵棤闄?
* direction: 'left',//婊氬姩鏂瑰悜锛?left','right','up','down'
* scrollamount:1,//姝ラ暱
* scrolldelay:20//鏃堕暱
*/
(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 = setinterval(scrollfunc, opts.scrolldelay);
//榧犳爣鍒掕繃鍋滄婊氬姩
$marquee.hover(
function(){
clearinterval(moveid);
},
function(){
clearinterval(moveid);
moveid = setinterval(scrollfunc, opts.scrolldelay);
}
);
});
};
$.fn.kxbdmarquee.defaults = {
isequal:true,//鎵€鏈夋粴鍔ㄧ殑鍏冪礌闀垮鏄惁鐩哥瓑,true,false
loop: 0,//寰幆婊氬姩娆℃暟锛?鏃舵棤闄?
direction: 'left',//婊氬姩鏂瑰悜锛?left','right','up','down'
scrollamount:1,//姝ラ暱
scrolldelay:20//鏃堕暱
};
$.fn.kxbdmarquee.setdefaults = function(settings) {
$.extend( $.fn.kxbdmarquee.defaults, settings );
};
})(jquery);