封装一个简单的banner轮播插件

一般普通企业网站都有一个首页轮播,为了实现这个功能,我们可以引用第三方插件(比如:swiper);当然,我们更感兴趣的是如何自己封装一个简单的轮播插件

废话少说,直接上码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {  margin: 0;  padding: 0;  font-family: '微软雅黑';  }
        a {  text-decoration: none;  }
        ul, ol, dl {  list-style-type: none;  }
        img {  border: none;  }
        input, button, a, img {  outline: none;  }
        html {  height: 100%;  }
        body {  height: 100%;  }

        .banner-box {  width: 100%;  margin: 0px auto;  overflow: hidden;  position: relative;  }
        .banner-box .list {  width: 1000%;  overflow: hidden;  position: relative;  left: 0;  top: 0;  }
        .banner-box .list li {  width: 10%;  float: left;  overflow: hidden;  }
        .banner-box .list li:first-child {  display: block;  }
        .banner-box .list li a img {  width: 100%;  display: block;  }
        .banner-box .prev,
        .banner-box .next {  position: absolute;  width: 100px;  height: 100px;  font-size: 50px;  line-height: 100px;  background-color: rgba(0, 0, 0, 0.4);  top: 50%;  margin-top: -50px;  color: #ffffff;  text-align: center;  cursor: pointer;  }
        .banner-box .prev:hover,
        .banner-box .next:hover {  background-color: rgba(0, 0, 0, 0.6);  }
        .banner-box .prev {  left: 0;  }
        .banner-box .next {  right: 0;  }
        .banner-box .banner-pages {  position: absolute;  bottom: 20px;  height: 20px;  width: 100%;  text-align: center;  font-size: 0;  }
        .banner-box .banner-pages span {  width: 20px;  height: 20px;  background-color: #cccccc;  display: inline-block;  border-radius: 50%;  -moz-border-radius: 50%;  -webkit-border-radius: 50%;  position: relative;  cursor: pointer;  margin: 0 6px;  line-height: 30px;  text-align: center;  z-index: 1;  font-size: 0px;  }
        .banner-box .banner-pages span.on {  background-color: #f99c38;  color: #ffffff;  }
    </style>
</head>
<body>
<div class="banner-box">
    <ul class="list">
        <li><a href="javascript:;"><img src="http://www.loushengyue.com/myself/img/index_banner1.jpg" alt="index_banner1.jpg"></a></li>
        <li><a href="javascript:;"><img src="http://www.loushengyue.com/myself/img/index_banner2.jpg" alt="index_banner2.jpg"></a></li>
        <li><a href="javascript:;"><img src="http://www.loushengyue.com/myself/img/index_banner3.jpg" alt="index_banner3.jpg"></a></li>
        <li><a href="javascript:;"><img src="http://www.loushengyue.com/myself/img/index_banner4.jpg" alt="index_banner4.jpg"></a></li>
    </ul>
    <div class="prev">&lt;</div>
    <div class="next">&gt;</div>
    <div class="banner-pages">
        <span class="on">1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
    </div>
</div>
<script src="http://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU=" crossorigin="anonymous"></script>
<script>
    ;(function (win, $) {
        var banner = function (obj) {
            this.bannerBox = $(obj);
            this.listBox = this.bannerBox.find('.list');
            this.list = this.bannerBox.find('.list li');
            this.prevBtn = this.bannerBox.find('.prev');
            this.nextBtn = this.bannerBox.find('.next');
            this.pages = this.bannerBox.find('.banner-pages span');
            this.index = 0;
            this.len = this.list.length;
            this.timer = null;
            this.isClicked = false;
            this.cloneOneToLast = this.list.eq(0).clone();
            this.cloneOneToLast.appendTo(this.listBox);
            this.nextBtnClickEvent();
            this.prevBtnClickEvent();
            this.pagesClickEvent();
            this.bannerBoxHoverEvent();
            this.autoPlay();
        };
        $.extend(true, banner.prototype, {
            runByIndex: function () {
                var _this = this;
                this.pages.removeClass('on');
                if (this.index == this.len || this.index == 0) {
                    this.pages.eq(0).addClass('on')
                } else {
                    this.pages.eq(this.index).addClass('on');
                }
                this.listBox.animate({left: -this.index * 100 + '%'}, 500, function () {
                    _this.isClicked = false;
                });
            },
            autoPlay: function () {
                var _this = this;
                _this.stopRun();
                _this.timer = setInterval(function () {
                    _this.nextRun();
                }, 3000);
            },
            stopRun: function () {
                if (this.timer) {
                    win.clearInterval(this.timer);
                }
            },
            nextRun: function () {
                if (this.index > this.len - 1) {
                    this.index = 0;
                    this.listBox.css('left', -this.index * 100 + '%');
                }
                this.index++;
                this.runByIndex();
            },
            prevRun: function () {
                if (this.index < 1) {
                    this.index = this.len;
                    this.listBox.css('left', -this.index * 100 + '%');
                }
                this.index--;
                this.runByIndex();
            },
            bannerBoxHoverEvent: function () {
                var _this = this;
                _this.bannerBox.hover(function () {
                    _this.stopRun();
                }, function () {
                    _this.autoPlay();
                });
            },
            pagesClickEvent: function () {
                var _this = this;
                _this.pages.click(function () {
                    if (!_this.isClicked) {
                        _this.index = $(this).index();
                        _this.runByIndex();
                    }
                });
            },
            prevBtnClickEvent: function () {
                var _this = this;
                _this.prevBtn.click(function () {
                    if (!_this.isClicked) {
                        _this.isClicked = true;
                        _this.prevRun();
                    }
                });
            },
            nextBtnClickEvent: function () {
                var _this = this;
                _this.nextBtn.click(function () {
                    if (!_this.isClicked) {
                        _this.isClicked = true;
                        _this.nextRun();
                    }
                });
            }
        });
        banner.init = function (obj) {
            for (var i = 0, len = obj.length; i < len; i++) {
                new this(obj[i]);
            }
        };
        win.MySelfBanner = banner;
    })(window, $);

    //实例化插件
    MySelfBanner.init($('.banner-box'));
</script>
</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值