js 行走的小女孩 面向对象

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
<style>

    * {
        margin:0;
        padding:0;
    }
    html,body {
        height:100%;
    }

    div.girl {
        width:79px;
        height:108px;
        background:url(aisidier.png) 0 -216px no-repeat;
        position:absolute;
        
    }
    
    div.end {
        border:2px solid #000;
        position:absolute;
        left:1000px;
        top:0;
        height:100%;
        
    }
</style>
</head>

<body>
    <div class="end"></div>
    
    <script>
        var girls = [];
        function Girls(){
            this.x = 0;
            this.y = parseInt(Math.random()*(document.documentElement.clientHeight - 108));
            this.speed = parseInt(Math.random()*10)+1;
            this.step = 0;
            this.isMove = true;
            this.init();
            this.update();
            this.bindEvent();
            girls.push(this);
        }
        
        Girls.prototype.init  = function(){
            this.dom = document.createElement('div');
            this.dom.className = "girl";
            document.body.appendChild(this.dom); //上树
        };
        
        Girls.prototype.update = function(){

            if(!this.isMove) 
                return;
             this.x += this.speed;
            if(this.x > 1000){
                this.goDeid();
            }
            this.step++;
            if(this.step > 7){
                this.step = 0;
            }
            this.dom.style.left = this.x+"px";
            this.dom.style.top = this.y+"px";
            console.log("left:",this.dom.style.left ,"top:",this.dom.style.top);
            this.dom.style.backgroundPosition = -this.step*79+"px -216px";
        };

        Girls.prototype.goDeid = function(){
            document.body.removeChild(this.dom); //下树
            for(var i=0;i<girls.length;i++){
                if(girls[i] == this){
                    girls.splice(i,1);
                }
            }
        };

        Girls.prototype.bindEvent = function(){
            var _this = this;
            this.dom.onclick = function(){
                _this.isMove = !_this.isMove;
            };
        };
        

        setInterval(function(){
            for(var i=0;i<girls.length;i++){
                girls[i].update();
            }
        },100);

        new Girls();
        new Girls();
        new Girls();
        new Girls();
        new Girls();
        new Girls();

    </script>
    
</body>
</html>

图片在文件中
复制代码

 

posted on   朝颜陌  阅读(555)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示