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>
图片在文件中
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)