美文网首页
无缝轮播图--面向对象编写

无缝轮播图--面向对象编写

作者: 阿布朗迪 | 来源:发表于2018-10-23 20:59 被阅读0次

利用面向对象编写无缝轮播图,需要三步走。
1、OOA 对轮播图进行分析

  • 选定元素
  • 绑定事件
  • 修改索引
  • 通过索引实现图片的播放
    2、OOD 对轮播图功能块进行设计
    3、OOP 详细地对功能块进行补充以达到最终目的
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>面向对象的轮播图</title>
        <style type="text/css">
            .cont{width: 1130px;height: 300px;margin: 50px auto;position:relative;overflow: hidden;}
            
            .imgbox{width: 5650px;position: absolute;left: 0;top: 0;}
            .imgbox a{float: left;width: 1130px;height: 300px;}
            
            .imgbox a img{width: 1130px;height: 300px;display: block;}
            
            .btns input{width: 50px;height: 50px;border: none;background: rgba(200,200,200,0.6);position: absolute;top: 125px;z-index: 9999999;}
            #prev{left: 0;}
            #next{right: 0;}
        </style>
    </head>
    <body>
        <div class="cont">
            <div class="imgbox">    
                <!-- <a href=""><img src="https://img.zcool.cn/community/0174f85b8422f2a8012190f2b97fa9.png@1380w"/></a> -->
                <a href=""><img src="https://img.zcool.cn/community/01c4005b863607a80120245cc788a1.jpg@1380w"/></a>
                <a href=""><img src="https://img.zcool.cn/community/01eb7b5b85ff52a80120245c031a6b.jpg@1380w"/></a>
                <a href=""><img src="https://img.zcool.cn/community/01327f5b8422c7a80120577d2aa796.png@1380w"/></a>
                <a href=""><img src="https://img.zcool.cn/community/0174f85b8422f2a8012190f2b97fa9.png@1380w"/></a>
                <a href=""><img src="https://img.zcool.cn/community/01c4005b863607a80120245cc788a1.jpg@1380w"/></a>
            </div>
            <div class="btns">
                <input type="button" name="" id="prev" value="<<<" />
                <input type="button" name="" id="next" value=">>>" />
            </div>
        </div>
    </body>
    <script src="move.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
    //OOA
        /*
            1、选元素
            2、绑定事件
            3、改变索引
            4、根据索引切换图片
        */
    //OOD
        function Banner(){
            this.cont = document.querySelector('.cont');
            this.next = document.querySelector('#next');
            this.prev = document.querySelector('#prev');
            this.imgBox  = document.querySelector('.imgbox');
            this.img = this.imgBox.children;
            this.index = 0;

            this.init()
        }

        Banner.prototype.init = function(){
            var that = this;
            this.next.onclick = function(){
                that.changeIndex("right")
            }
            this.prev.onclick = function(){
                that.changeIndex("left")
            }
        }
        Banner.prototype.changeIndex = function( direction ){
            if(direction == 'right'){
                if( this.index == this.img.length - 1){
                    this.index = 1;
                    this.imgBox.style.left = 0;
                }else{
                    this.index++;
                    
                }
            }else{
                if( this.index == 0){
                    this.index = this.img.length-2;
                    this.imgBox.style.left = -this.img[0].offsetWidth * (this.index+1) + 'px';
                    console.log( this.index)
                }else{
                    this.index--;
                    // console.log( this.index)
                }
            }
            this.move();
        }
        Banner.prototype.move = function(){
            move(this.imgBox,{'left':-this.img[0].offsetWidth*this.index})
        }

        new Banner();
    //OOP
    </script>
</html>


相关文章

  • 无缝轮播图--面向对象编写

    利用面向对象编写无缝轮播图,需要三步走。1、OOA 对轮播图进行分析 选定元素 绑定事件 修改索引 通过索引...

  • 2019-04-09

    轮播图 内置对象 面向对象 工厂模式 创建空对象 prototype 原型 var o = new Object(...

  • 无标题文章

    轮播图分为:传统轮播图、间歇轮播图、呼吸轮播图、无缝滚动轮播图等。它们各具特色,各有用处。 1.传统轮播图 第一步...

  • 轮播图

    轮播图分为:传统轮播图、间歇轮播图、呼吸轮播图、无缝滚动轮播图等。 1.传统轮播图 第一步,得到元素 第二步,设置...

  • JS中常见的几种轮播

    轮播图 1.无缝不停轮播 ​ 1. body布局: ​ 2. css样式: 3. JS代码: 2.无缝停顿...

  • jQuery实现轮播图无缝(无回滚)滚动切换效果

    HTML 无缝切换轮播图