jquery幻燈片淡入淡出組件封裝

效果圖javascript

slider.htmlcss

<!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/slider.css">
</head>
<body>
    <div class="slider">
        <div class="slider-img slide-fade">
            <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">&lt;</a>
        <a href="javascript:;" class="slider-arrow slider-arrow-right">&gt;</a>
    </div>

    <script src="../js/jquery.js"></script>
    <script src="../js/transition.js"></script>
    <script src="../js/showhide.js"></script>
    <script src="../js/slider.js"></script>
    <script>
        //傳入自定義參數
        $(".slider").Slider({
            css3:true,
            js:true,
            animation:"fade",
            activeIdx:0,//從哪張開始輪播
            interval:1000//300毫秒的速度輪播        
        });

        //接收消息,實現按需加載(幻燈片接收消息,父元素實現事件代理)
        $(".slider-img").on("slider-show",function(e,i,elem){
            console.log(i);//裏面的事件根據本身需求來寫
        });
    </script>
</body>
</html>

base.css  http://www.javashuo.com/article/p-hddyogwd-by.htmlhtml

slider.cssjava

    /*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;
    }
    .slide-fade .slider-img-item{
        width:100%;
        height:100%;
        position: absolute;
        top:0;
        left:0;    
        display: none;    
    }
    .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%;
    }
    .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  http://www.javashuo.com/article/p-nbnfsgpd-w.htmljquery

showhide.js  http://www.javashuo.com/article/p-cgnrmqkl-h.htmlcss3

slider.jsweb

(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.curIdx=this._getIdx(this.options.activeIdx);
        this.size=this.pics.length;

        this._init();//初始化,下劃線開頭一般約定爲僅供內部使用
    }
    //默認參數
    Slider.defaults={
        css3:false,
        js:false,
        animation:"fade",
        activeIdx:0,//從哪張開始輪播
        interval:0//默認沒有自動輪播
    };
    //初始化
    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"){
            this.picBox.addClass("slide-slide");
            this.to=this._slide;
        
        }else{            
            this.picBox.addClass("slide-fade");
            //顯示指定圖片
            this.pics.eq(this.curIdx).show();
            this.to=this._fade;
        }

        //showHide init控制幻燈片的顯示隱藏
        this.pics.showHide(this.options);

        //鼠標移入顯示左右箭頭
        this.elem.hover(function(){
            self.arrows.show();
        },function(){
            self.arrows.hide();
        }).on("click",".slider-arrow-left",function(){
            //事件代理
            self.to(self._getIdx(self.curIdx-1));
        }).on("click",".slider-arrow-right",function(){
            self.to(self._getIdx(self.curIdx+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();
        }

        //發送消息(調用showhide模塊的是幻燈片元素,所以發送接收消息的也是幻燈片元素)
        this.pics.on("show shown hide hidden",function(e){
            self.pics.trigger("slider-"+e.type,[self.pics.index(this),this]);
            //返回的數組包含當前圖片的索引+觸發事件的對象
        })

    };
    //獲取合理的索引
    Slider.prototype._getIdx=function(index){
        if(isNaN(Number(index))) return 0;
        if(Number(index)<0) return this.size-1;
        if(Number(index)>this.size-1) return 0;
        return index;
    }
    //淡入淡出方式
    Slider.prototype._fade=function(index){
        if(this.curIdx===index) return;//點擊當前索引再也不切換

        this.pics.eq(this.curIdx).showHide("hide");
        this.pics.eq(index).showHide("show");

        this.tips.eq(this.curIdx).removeClass("slider-tip-item-active");
        this.tips.eq(index).addClass("slider-tip-item-active");

        this.curIdx=index;
        
    }
    //滑動方式
    Slider.prototype._slide=function(){

        
    }
    //自動
    Slider.prototype.auto=function(){
        var self=this;
        this.timer=setInterval(function(){
            self.to(self._getIdx(self.curIdx+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做爲參數傳入,則$符做爲內部變量,不會發生衝突
相關文章
相關標籤/搜索