效果圖 slider2.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>slider</title> <link rel="stylesheet" href="../css/base.css"> < ...
效果圖
slider2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>slider</title> <link rel="stylesheet" href="../css/base.css"> <link rel="stylesheet" href="../css/slider2.css"> </head> <body> <div class="slider"> <div class="slider-img slide-slide"> <a href="#" class="slider-img-item"><img src="../img/focus-slider/1.png"></a> <a href="#" class="slider-img-item"><img src="../img/focus-slider/2.png"></a> <a href="#" class="slider-img-item"><img src="../img/focus-slider/3.png"></a> <a href="#" class="slider-img-item"><img src="../img/focus-slider/4.png"></a> </div> <ol class="slider-tip"> <li class="slider-tip-item text-hidden fl">1</li> <li class="slider-tip-item text-hidden fl">2</li> <li class="slider-tip-item text-hidden fl">3</li> <li class="slider-tip-item text-hidden fl">4</li> </ol> <a href="javascript:;" class="slider-arrow slider-arrow-left"><</a> <a href="javascript:;" class="slider-arrow slider-arrow-right">></a> </div> <script src="../js/jquery.js"></script> <script src="../js/transition.js"></script> <script src="../js/showhide.js"></script> <script src="../js/move.js"></script><!-- 滑動模塊 --> <script src="../js/slider2.js"></script> <script> $(".slider").Slider({ css3:true, js:true, animation:"slide", activeIdx:0,//從哪張開始輪播 interval:1000,//1秒的速度輪播 loop:true//是否設置無縫迴圈 }); //接收消息 $(".slider-img").on("slider-show slider-shown slider-hide slider-hidden",function(e,i,elem){ console.log(i+":"+e.type);//裡面的事件根據自己需求來寫 }); </script> </body> </html>
base.css https://www.cnblogs.com/chenyingying0/p/12363689.html
slider2.css
/*showhide*/ .fadeOut{ opacity: 0; visibility: hidden; } .slideUpDownCollapse{ height:0 !important;/*避免因為優先順序不夠而無法生效*/ padding-top:0 !important; padding-bottom:0 !important; } .slideLeftRightCollapse{ width:0 !important;/*避免因為優先順序不夠而無法生效*/ padding-left:0 !important; padding-right:0 !important; } .fl{ float:left; } .fr{ float:right; } /*提取出過渡樣式,可公用*/ .transition{ -webkit-transition:all .5s; -moz-transition:all .5s; -ms-transition:all .5s; -o-transition:all .5s; transition:all .5s; } /*文字隱藏*/ .text-hidden{ text-indent:-9999px; overflow:hidden; } .slider{ width:728px; height:504px; position: relative; overflow:hidden; } .slider-img{ width:100%; height:100%; position: relative; } /*fade方式*/ .slide-fade .slider-img-item{ width:100%; height:100%; position: absolute; top:0; left:0; display: none; } /*slide方式*/ .slide-slide{ position: absolute; top:0; left:0; width:1000%;/*夠用就行,具體寬度在js中獲取*/ height:100%; background-color:pink; } .slide-slide .slider-img-item{ float:left; } .slider-tip{ position: absolute; height:12px; width:78px; bottom:24px; left:50%; margin-left:-36px; } .slider-tip-item{ width:8px; height:8px; border:2px solid #e4e8eb; background-color: #313a43; margin-right:10px; border-radius:50%; cursor:pointer; } .slider-tip-item-active{ background-color:#e4e8eb; border:2px solid #313a43; } .slider-tip-item:last-child{ margin-right:0; } .slider-arrow{ display: none; position: absolute; width:30px; height:40px; line-height:40px; top:50%; margin-top:-20px; background-color:rgba(0,0,0,.5); color:#e4e8eb; text-align: center; font-size:20px; font-family:simsum; } .slider-arrow-left{ left:0; } .slider-arrow-right{ right:0; }
transition.js https://www.cnblogs.com/chenyingying0/p/12363649.html
showhide.js https://www.cnblogs.com/chenyingying0/p/12363707.html
move.js https://www.cnblogs.com/chenyingying0/p/12380632.html
slider2.js
(function($){ 'use strict';//使用嚴格模式 //構造函數形式 function Slider(elem,options){ //保存到this中才能公用 this.elem=elem; this.picBox=this.elem.find(".slider-img"); this.options=options; this.pics=this.elem.find(".slider-img-item"); this.tips=this.elem.find(".slider-tip-item"); this.arrows=this.elem.find(".slider-arrow"); this.size=this.pics.length;//圖片數量 this.curIdx=this._getIdx(this.options.activeIdx); this._init();//初始化,下劃線開頭通常約定為僅供內部使用 } //預設參數 Slider.defaults={ css3:false, js:false, animation:"fade", activeIdx:0,//從哪張開始輪播 interval:0,//預設沒有自動輪播 loop:false }; //初始化 Slider.prototype._init=function(){ var self=this; //顯示指定圓點 this.tips.removeClass("slider-tip-item-active"); this.tips.eq(this.curIdx).addClass("slider-tip-item-active"); //指定動畫方式 to if(this.options.animation==="slide"){ //slide this.picBox.addClass("slide-slide"); this.to=this._slide; //根據參數初始化顯示某一張 this.picWidth=this.pics.eq(0).width();//一張圖片的寬度 this.picBox.css("left",-1*this.curIdx*this.picWidth);//圖片容器要移動的距離 //move init this.picBox.move(this.options); if(this.options.loop){ //添加clone()是新克隆一份添加到最後 //沒有clone()的話,最前面的一張會消失,添加到最後 this.picBox.append(this.pics.eq(0).clone()); this.transitionCls=this.picBox.hasClass("transition")?"transition":""; this.size++;//圖片數量加1 } }else{ //fade this.picBox.addClass("slide-fade"); //顯示指定圖片 this.pics.eq(this.curIdx).show(); //fade 模塊 發送消息(調用showhide模塊的是幻燈片元素,因此發送接收消息的也是幻燈片元素) this.pics.on("show shown hide hidden",function(e){ self.pics.trigger("slider-"+e.type,[self.pics.index(this),this]); //返回的數組包含當前圖片的索引+觸發事件的對象 }) //showHide init控制幻燈片的滑動 this.pics.showHide(this.options); this.to=this._fade; } //滑鼠移入顯示左右箭頭 this.elem.hover(function(){ self.arrows.show(); },function(){ self.arrows.hide(); }).on("click",".slider-arrow-left",function(){//左箭頭,圖片left為正 //事件代理 self.to(self._getIdx(self.curIdx-1),1);//第二個參數代表滑動時的方向 }).on("click",".slider-arrow-right",function(){//右箭頭,圖片left為負 self.to(self._getIdx(self.curIdx+1),-1); }).on("click",".slider-tip-item",function(){ self.to(self._getIdx(self.tips.index(this))); //self.tips.index(this)是當前點擊的圓點的索引 }); //auto if(this.options.interval && !isNaN(Number(this.options.interval))){ //滑鼠移入時暫停自動切換 this.elem.hover($.proxy(this.pause,this),$.proxy(this.auto,this)); self.auto(); } }; //獲取合理的索引 Slider.prototype._getIdx=function(index,maxNum){ maxNum=maxNum || this.size;//手動設置maxNum代表用的是無縫切換模式 if(isNaN(Number(index))) return 0; if(Number(index)<0) return maxNum-1; if(Number(index)>maxNum-1) return 0; return index; } //激活小圓點 Slider.prototype._activateTips=function(index){ this.tips.removeClass("slider-tip-item-active");//全部小圓點先移除樣式 this.tips.eq(index).addClass("slider-tip-item-active"); } //淡入淡出方式 Slider.prototype._fade=function(index){ if(this.curIdx===index) return;//點擊當前索引不再切換 this.pics.eq(this.curIdx).showHide("hide"); this.pics.eq(index).showHide("show"); this._activateTips(index); this.curIdx=index; } //滑動方式 Slider.prototype._slide=function(index,direction){ var self=this; if(this.curIdx===index) return; this.picBox.move("x",-1*index*this.picWidth);//移動指定的位置 this.curIdx=index; //direction代表是否左右箭頭點擊 //如果是點擊的小圓點,不需要處理無縫切換 if(this.options.loop && direction){ if(direction<0){//click right if(index===0){ this.picBox.removeClass(this.transitionCls).css("left",0);//瞬間從新增的一張切換到第一張 this.curIdx=index=1; setTimeout(function(){ self.picBox.addClass(self.transitionCls).move("x",-1*index*self.picWidth); },20) } }else{//click left if(index===this.size-1){ this.picBox.removeClass(this.transitionCls).css("left",-1*index*self.picWidth);//瞬間從新增的一張切換到第一張 this.curIdx=index=this.size-2;//從新增的一張往最後一張移動 setTimeout(function(){ self.picBox.addClass(self.transitionCls).move("x",-1*index*self.picWidth); },20) } } //如果選擇無縫切換 index=this._getIdx(index,this.size-1); } this._activateTips(index);//激活小圓點 } //自動 Slider.prototype.auto=function(){ var self=this; this.timer=setInterval(function(){ self.to(self._getIdx(self.curIdx+1),-1); },self.options.interval); } //停止 Slider.prototype.pause=function(){ clearInterval(this.timer); } //插件形式 $.fn.extend({ Slider:function(opt){ //return this可以返回對象,方便連寫 return this.each(function(){ var ui=$(this); var slider=ui.data("slider"); //opt是參數對象 var options=$.extend({},Slider.defaults,ui.data(),typeof opt==="object"&&opt); //單例:一個DOM元素對應一個實例,如果已經存在則不需要反覆實例化 if(!slider){ slider=new Slider(ui,options); ui.data("slider",slider); } //opt是show或者hide if(typeof slider[opt]==="function"){ Slider[opt](); } }); } }); })(jQuery); //為了防止$符發生衝突,將jQuery作為參數傳入,則$符作為內部變數,不會發生衝突