图片轮播之插件封装

我封装的这个轮播插件只需要获取到图片和按钮就可以啦。

css 样式

.body{
        width: 700px;
        margin: 100px auto;
        position: relative;
        height: 300px;
        overflow: hidden;
    }
    .body img{
        width: 700px;
        position: absolute;
        display: none;
    }
    .body ul{
        position: absolute;
        bottom: 3px;
        left: 50%;
        transform: translateX(-50%);

    }
    .body li{
        list-style: none;
        float: left;
        width: 15px;
        height: 15px;
        border-radius: 50px;
        background: none;
        border: 2px solid #fff;
        margin-right: 10px;
        cursor: pointer;
    }
    .active{
        background-color: #fff !important;

    }
    .body a{
        text-decoration: none;
        position: absolute;
        display: block;
        top: 50%;
        transform: translateY(-50%);
        height: 50px;
        width: 30px;
        background-size: 100% 60%;
        background-color: rgba(0,0,0,0.3);
    }
    .left{
        left: 0;
        background: url('../img/left.png') no-repeat center center;
    }
    .right{
        right: 0;
        background: url('../img/right.png') no-repeat center center;
    }

 

页面结构 html 代码

<body>
    <div class="body">

       <img src="img/1.jpg">
       <img src="img/2.jpg">
       <img src="img/3.jpg">

<ul>
            <li class="active"></li>
            <li></li>
            <li></li>
        </ul>
        <a href="javascript:;" class="left"></a>
        <a href="javascript:;" class="right"></a>
    </div>

js部分,插件调用

<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="js/slider.js"></script>
    <script type="text/javascript">
    $.slider({
        imgElement:$(".body img"),
        liElement:$(".body li"),
        leftBtn:$(".left"),
        rightBtn:$(".right"),
        time:2000
    })
</script>
</body>

 

封装的插件

(function($){
    function slider(options){
        this.opts=$.extend({},slider.defaluts,options);
        this._imgSlider();
    }
    //设置默认值
    slider.defaluts={
        imgElement:null,
        liElement:null,
        leftBtn:null,
        rightBtn:null,
        time:2000
    }
    slider.prototype._imgSlider=function(){
        var opts=this.opts,
            index=0,
            len=opts.imgElement.length,
            timeInter=null;
        if(opts.imgElement=='') return;
        opts.imgElement.eq(0).show();
        showTime();
          //当鼠标经过  轮播停止,移开继续
          opts.imgElement.hover(function() {
              clearInterval(timeInter);
          }, function() {
              showTime();
          });

        //点击li  显示对应的图片
        opts.liElement.click(function(){
            var id=$(this).index();
            show(id);
        });
        //向左向右
        opts.leftBtn.click(function(){
            clearInterval(timeInter);
            --index;
            index=Math.max(0,index);
            show(index);
            showTime();
        });
        opts.rightBtn.click(function(){
            clearInterval(timeInter);
            ++index;
            index=Math.min(len-1,index);
            show(index);
            showTime();
        });


        function showTime(){
            timeInter=setInterval(function(){
                index++;
                if(index>len){
                    index=0;
                }
                show(index);
            },opts.time);
        }
        function show(index){
            opts.imgElement.eq(index).fadeIn(1000).siblings('img').fadeOut(1000);
            opts.liElement.eq(index).addClass('active').siblings('li').removeClass('active');
        }

    }
    $.extend({
        slider:function(options){
            new slider(options);
        }
    })
})(jQuery)

效果图

 

转载于:https://www.cnblogs.com/xiaohanh/p/7216483.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值