• JS中的混合模式


    function Animation(list) {
        this.box = document.getElementById(list.id);
        this.size = list.size;
        this.url = list.url;
        
        this.init()            //    Animation.prototype中的init(),初始化一些值(非私有)
        this.DOMready();    //    调用Animation.prototype中的 DOMready(),每一个new Animation()都会执行这里面的语句
    }
    
    Animation.prototype = {
        //    1.init:初始化一些非私有值
        init:function(){
            this.width  = box.clientWidth;
            this.height = box.clientHeight;
        },
        
       //    2.DOMready:用来生成div的,用到定时器,循环设置
        DOMready: function(){
            var box = this.box;
            var size = this.size;
            // 下面保存了this(Animation)到self之中,因为等会要调用Animation.DOMmove(cirDiv)
            var self = this;
            
            var timer = setInterval(function(){
                var top = 0;
                var xPosition = Math.random()*(self.width - self.size);
                var cirDiv = document.createElement("div");
                cirDiv.style.cssText = "background:"+LYX_Colors_getRandomColor()+";"+"left:"+xPosition+"px;"+ "25px;height:25px;border-radius: 50%;position: absolute;";
                box.appendChild(cirDiv);
                
                //    调用Animation.DOMmove(cirDiv),给每个div设置定时器,用来调整top值
                self.DOMmove(cirDiv)        
            }, 300)
        },
        
        //    3.调整top值函数
        DOMmove: function(cirDiv) {
            var top = 0;
            var self = this;
            var cirDiv = cirDiv;
            var timer_2 = setInterval(function() {
                top++;
                cirDiv.style.top = top+"px";
                if (top > self.height - self.size) {
                    box.removeChild(cirDiv);
                    clearInterval(timer_2);
                }
            },1)
        }
    }
    
     
    
    //    新建s调用方法
    var s = new Animation({
        id: "box",
        size: "10"
    });
  • 相关阅读:
    避免前置声明
    CLion在WSL上远程调试代码设置
    push_back与构造函数
    _BLOCK_TYPE_IS_VALID(pHead->nBlockUse问题解析
    Qt报错
    关于引用与指针实现多态的一些记录
    Vue-Axios异步通信
    Kafka概述
    学习Ajax看着一篇就够了
    学习Json看着一篇就够了
  • 原文地址:https://www.cnblogs.com/nemoro1928/p/5383849.html
Copyright © 2020-2023  润新知