/** * author: happynetlife@qq.com * date: 2018.04.10 淇锛?021-02-01 * option: * speed 閫熷害锛岄粯璁?0 * direction 鏂瑰悜锛岄粯璁eft锛屽悜涓婁负top * border 杈规锛岄粯璁?,鏈夋椂鍐呴儴li瀹氫箟浜嗚竟妗嗭紝瀵艰嚧婊氬姩鍑虹幇鍋忓樊锛屾鏃跺彲浠ヨ缃竟妗嗙偣鐢ㄧ殑楂樺害鎴栧搴︼紝娉ㄦ剰锛屽悜宸︽粴鍔ㄨ绠楀乏鍙充袱杈癸紝鍚戜笂婊氬姩璁$畻涓婁笅涓よ竟锛岃繖閲岀殑border鍊煎簲璇ユ槸涓よ竟涔嬪拰 * 婊氬姩瀹瑰櫒蹇呴』瑕佽缃湁鏁堢殑width鎴杊eight鎵嶈兘姝e父婊氬姩 */ ;(function($){ $.fn.Marquee = function(options){ //鎾斁瀹瑰櫒瀵硅薄 const $this = $(this); const $ul = $('ul',$this); //濡傛灉鎵句笉鍒皍l鍏冪礌鍒欏仠姝 if($ul.length === 0)return this; //li鍏冪礌 let $lis = $('li',$ul); //li鍏冪礌鐨勬暟閲 let liSize = $lis.length; //灏戜簬涓€涓笉婊氬姩 if(liSize < 1)return this; //婊氬姩鍖哄煙 let $MC = $this.css('overflow','hidden'); //榛樿鍊 const def = { speed:40, border:0, inline:false, direction:"left" }; //鍙傛暟鎵╁睍 const opt = $.extend({},def,options); //li鍏冪礌鐨勫 let lW = $lis.eq(0).width() + opt.border; //濡傛灉鏄鍐呭厓绱狅紝鍒欏彇li鍏冪礌瀹藉害鐨勫钩鍧囧€ if(opt.inline){ let uW = 0; $lis.each(function(){ uW += $(this).width(); }); lW = uW / liSize; } //li鍏冪礌鐨勯珮 let lH = $lis.eq(0).height() + opt.border; let myMarquee = null; let scrollAction = null; //鍚戝乏婊氬姩鍑芥暟 let scrollLeft = function(){ if($MC.scrollLeft() >= (liSize * lW)){ $MC.scrollLeft(0); }else{ $MC.scrollLeft($MC.scrollLeft()+1); } }; //鍚戜笂婊氬姩鍑芥暟 let scrollTop = function(){ if($MC.scrollTop() >= (liSize * lH)){ $MC.scrollTop(0); }else{ $MC.scrollTop($MC.scrollTop()+1); } }; switch(opt.direction){ case 'top': //li鍏冪礌鎬婚珮搴﹀ぇ浜庡鍣ㄦ墠婊氬姩 if(liSize * lH >= $MC.height()){ //璁剧疆ul鐨勫搴︿负 li瀹藉害 * li鏁伴噺 * 2 $ul.height(liSize * lH * 2).append($lis.clone()); //婊氬姩鍑芥暟 scrollAction = scrollTop; } break; default: //li鍏冪礌鎬诲搴﹀ぇ浜庡鍣ㄦ墠婊氬姩 if(liSize * lW >= $MC.width()){ //璁剧疆ul鐨勫搴︿负 li瀹藉害 * li鏁伴噺 * 2 $ul.width(liSize * lW * 2).append($lis.clone()); //婊氬姩鍑芥暟 scrollAction = scrollLeft; } } //婊氬姩鍑芥暟涓嶄负绌 if(scrollAction !== null){ myMarquee = setInterval(scrollAction,opt.speed); //榧犳爣杩涘叆鍜岀寮€ $ul.hover( function(){ clearInterval(myMarquee); }, function(){ myMarquee = setInterval(scrollAction,opt.speed); } ); } //杩斿洖this,浣块摼寮忔搷浣滄敮鎸 return this; } })(jQuery);