就是一个小老鼠,它会跟着你的鼠标移动,左边有一个食物罐子,你单击,就会从中洒出一个圆点(食物),
当小老鼠靠近圆点时,它就会自动吃。右边是一个圆形的东西,你单击它,小老鼠就会
跑到里面,在其中不停地跑动,我需要具体怎么做。我是初学者,希望老师们能讲的详细一些。需要用什么语言,软件做。。之类的,尽量详细一些,谢谢。
在网页上做出这样一个小程序,老鼠跟随鼠标移动的问题。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- tsinggao 2015-08-09 04:35关注
直接上代码
<!DOCTYPE html> <html> <head> <script src="jquery.js"> </script> <style> #mouse{ position:absolute; border: 1px solid #fd3; left: 100px; top: 100px; } </style> <script> var cursorX = ''; var cursorY = ''; $(document).ready(function(){ init(); }); $(document).ready(function(){ setInterval("updateMouse()",10); }); function init(){ document.body.onmousemove = function(e){ if (!e) { e = window.event; } else { e.srcElement = e.target; } cursorX = e.clientX; cursorY = e.clientY; document.getElementById("debug").innerHTML = "cursorX:" + cursorX + " cursorY:" + cursorY; }; } function updateMouse(){ var mouse = document.getElementById("mouse"); var mouseX = mouse.offsetLeft; var mouseY = mouse.offsetTop; if((cursorX-mouseX)>10){ mouse.style.left = mouseX + 1 + "px"; } if((cursorX-mouseX)<-10){ mouse.style.left = mouseX - 1 + "px"; } if((cursorY-mouseY)>10){ mouse.style.top = mouseY + 1 + "px"; } if((cursorY-mouseY)<-10){ mouse.style.top = mouseY - 1 + "px"; } document.getElementById("debug2").innerHTML = "cursorX:"+cursorX+ " cursorY:"+cursorY + "<br>mouseX:"+mouseX+ " mouseY:"+mouseY ; } </script> </head> <body > <div id="root" style="width:1200px;height:600px;border:1px solid #CCC"> <div id="mouse"> mouse </div> <div id="debug" style="position:fixed;right:200px;top:200px;width:300px;height:200px;border:1px solid #CCC"> </div> <div id="debug2" style="position:fixed;right:200px;top:500px;width:300px;height:200px;border:1px solid #CCC"> </div> <div id="food" style="position:fixed;left:100px;top:200px;width:100px;height:100px;border:1px solid #CCC">食物 </div> <div id="cage" style="position:fixed;right:800px;top:200px;width:100px;height:100px;border:1px solid #CCC">笼子 </div> </div> </body> </html>
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1
悬赏问题
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效
- ¥15 悬赏!微信开发者工具报错,求帮改