本文為大家示範以 JS 實作的網頁遊戲中滑塊響應滑鼠點擊移動效果,希望對學習中的朋友有所幫助。
這是網頁遊戲中的一個有趣效果,可以完成滾動、8 個方向的 scroll、滑鼠座標獲取、物件上下左右位置獲取等動作,大家可以自行添加。程式碼中 MoveReady 與 ScrollReady 兩處註解若取消,可防止滑鼠連續點擊造成效果疊加。
線上演示網址如下:
https://demo.jb51.net/js/2015/js-web-game-click-move-demo/
具體程式碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>網頁遊戲中的一個有趣效果</title>
<style type="text/css">
body{font:12px/24px "宋體";background:#fff;color:#333;}
.block{width:200px;height:200px;background:#e63;position:absolute;left:50%;top:50%;border:3px solid #fdd;overflow:hidden;}
.moveBlock{width:1900px;height:2728px;background:url("https://files.jb51.net/file_images/article/201510/2015101993654799.jpg") left top;}
</style>
<script type="text/javascript">
var ie = ! -[1, ], myDate = new Date();
function Sin(x,v) {
var y = Math.round(Math.sin(Math.PI*x/180)*100)/100;
return ie?y*v*3:y*v;
}
var div=function(id){return new HtmlEle.init(id);}
HtmlEle={
init:function(id){
this[0]=id==Object?id:document.getElementById(id);
return this;
},
css:function(s){ for(var i in s){ this[0].style[i]=s[i]; } },
Obj:function(){
var obj=this[0];
var objTop=obj.offsetTop,objLeft=obj.offsetLeft,objWidth=obj.offsetWidth,objHeight=obj.offsetHeight;
return {top:objTop,right:objLeft+objWidth,bottom:objTop+objHeight,left:objLeft,width:objWidth,height:objHeight}
},
MoveReady:true,
Move:function(s,point){
var obj=this[0],n=0,v=s,main=this,v2=90;
if(this.MoveReady==true){
var Top=function(){ n=n<v2?n+1:n; var m=Sin(n,v); if(m<Math.abs(s)){obj.style.top=P-m+"px"}else{obj.style.top=P-s+"px";clearInterval(myMove);main.MoveReady=true;} }
var Right=function(){ n=n<v2?n+1:n; var m=Sin(n,v); if(m<Math.abs(s)){obj.style.left=P+m+"px"}else{obj.style.right=P+s+"px";clearInterval(myMove);main.MoveReady=true;} }
var Bottom=function(){ n=n<v2?n+1:n; var m=Sin(n,v); if(m<Math.abs(s)){obj.style.top=P+m+"px"}else{obj.style.top=P+s+"px";clearInterval(myMove);main.MoveReady=true;} }
var Left=function(){ n=n<v2?n+1:n; var m=Sin(n,v); if(m<Math.abs(s)){obj.style.left=P-m+"px"}else{obj.style.left=P-s+"px";clearInterval(myMove);main.MoveReady=true;} }
switch(point){
case "top" : var P=obj.offsetTop,Point=Top;break;
case "right" : var P=obj.offsetLeft,Point=Right;break;
case "bottom" : var P=obj.offsetTop,Point=Bottom;break;
case "left" : var P=obj.offsetLeft,Point=Left;break;
}
var myMove=setInterval(Point,5)
}
},
ScrollReady:true,
Scroll:function(s,point){
var obj=this[0],n=0,v=s,main=this,v2=90;
if(this.ScrollReady==true){
var Top=function(){ n=n<v2?n+1:n; var m=Sin(n,v); if(m<Math.abs(s)){obj.scrollTop=P+m}else{obj.scrollTop=P+s;clearInterval(myMove);main.ScrollReady=true;} }
var Right=function(){ n=n<v2?n+1:n; var m=Sin(n,v); if(m<Math.abs(s)){obj.scrollLeft=P-m}else{obj.scrollLeft=P-s;clearInterval(myMove);main.ScrollReady=true;} }
var Bottom=function(){ n=n<v2?n+1:n; var m=Sin(n,v); if(m<Math.abs(s)){obj.scrollTop=P-m}else{obj.scrollTop=P-s;clearInterval(myMove);main.ScrollReady=true;} }
var Left=function(){ n=n<v2?n+1:n; var m=Sin(n,v); if(m<Math.abs(s)){obj.scrollLeft=P+m}else{obj.scrollLeft=P+s;clearInterval(myMove);main.ScrollReady=true;} }
switch(point){
case "top" : var P=obj.scrollTop,Point=Top;break;
case "right" : var P=obj.scrollLeft,Point=Right;break;
case "bottom" : var P=obj.scrollTop,Point=Bottom;break;
case "left" : var P=obj.scrollLeft,Point=Left;break;
}
var myMove=setInterval(Point,5)
}
},
Mouse:function(e,f){
switch(e){
case "click" : this[0].onclick=f;break;
case "dbclick" : this[0].ondblclick=f;break;
case "over" : this[0].onmouseover=f;break;
case "out" : this[0].onmouseout=f;break;
}
},
MouseSite:function(e){ var e=e?e:window.event; return {x:e.clientX,y:e.clientY} }
}
HtmlEle.init.prototype=HtmlEle;
</script>
</head>
<body>
<div class="block" id="moveBlock"><div class="moveBlock"></div></div>
<script type="text/javascript">
var sBlock=new div("moveBlock");
document.onclick=function(){
var site=sBlock.MouseSite(arguments[0]),s=200,b=30;
var objSite=sBlock.Obj();
if(objSite.left<site.x && objSite.left+objSite.width/2-b>site.x && objSite.top<site.y && objSite.bottom>site.y) sBlock.Scroll(s,"left");
if(objSite.right>site.x && objSite.right-objSite.width/2+b<site.x && objSite.top<site.y && objSite.bottom>site.y) sBlock.Scroll(s,"right");
if(objSite.top<site.y && objSite.top+objSite.height/2-b>site.y && objSite.left<site.x && objSite.right>site.x) sBlock.Scroll(s,"top");
if(objSite.bottom>site.y && objSite.bottom-objSite.height/2+b<site.y) sBlock.Scroll(s,"bottom");
if(objSite.left>site.x) sBlock.Move(s,"left");
if(objSite.right<site.x) sBlock.Move(s,"right");
if(objSite.top>site.y) sBlock.Move(s,"top");
if(objSite.bottom<site.y) sBlock.Move(s,"bottom");
}
window.onload=function(){sBlock[0].scrollTop=0;sBlock[0].scrollLeft=0;}
</script>
</body>
</html>
希望本文所述能對大家的 JavaScript 程式設計有所幫助。