JS实现网页游戏中滑块响应鼠标点击移动效果
本文实例讲述了JS实现网页游戏中滑块响应鼠标点击移动效果。分享给大家供大家参考,具体如下:
这是网页游戏中的一个有趣效果,可以完成以下几个动作:滚动、scroll8个方向、鼠标坐标获娶对象上下左右位置获取,大家可以自己添加、MoveReady和ScrollReady两个注释掉了取消注释后防止鼠标连续点击效果叠加。
在线演示地址如下:
http://demo.jb51.net/js/2015/js-web-game-click-move-demo/
具体代码如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"><head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>网页游戏中的一个有趣效果</title>
<styletype="text/css">
html,body,div,span,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,address,big,cite,del,em,img,ins,small,strong,sub,sup,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,tbody,tfoot,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent;}
body{font:12px/24px"宋体";background:#fff;color:#333;}
ol,ul{list-style:none;}
blockquote,q{quotes:none;}
blockquote:before,blockquote:after,
q:before,q:after{content:'';content:none;}
:focus{outline:0;}
ins{text-decoration:none;}
del{text-decoration:line-through;}
table{border-collapse:collapse;border-spacing:0;}
label{cursor:pointer}
input[type="button"]{cursor:pointer}
button{cursor:pointer}
strong{font-weight:normal;}
em{font-style:normal}
a{text-decoration:none;color:#f60}
a:hover{text-decoration:underline;}
.block{width:200px;height:200px;background:#e63;position:absolute;left:50%;top:50%;border:3pxsolid#fdd;overflow:hidden;}
.moveBlock{width:1900px;height:2728px;background:url("//img.jbzj.com/file_images/article/201510/2015101993654799.jpg")lefttop;}
</style>
<scripttype="text/javascript">
varie=!-[1,],myDate=newDate();
functionSin(x,v){
vary=Math.round(Math.sin(Math.PI*x/180)*100)/100;
returnie?y*v*3:y*v;
}
/*返回数组序号*/
Num=function(e,eles){
for(vari=0;i<eles.length;i++){
if(eles[i]==e){returni;}
}
}
/*字符串判定*/
GetCur=function(Str,cur){
varstr=newRegExp(cur,"gi")
returnstr.test(Str);
}
/*DOM构造*/
vardiv=function(id){returnnewHtmlEle.init(id);}
HtmlEle={
init:function(id){
this[0]=id==Object?id:document.getElementById(id);
returnthis;
},
css:function(s){
for(variins){
this[0].style[i]=s[i];
}
},
Obj:function(){
varobj=this[0];
varobjTop=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){
varobj=this[0],n=0,v=s,main=this,v2=90;
if(this.MoveReady==true){
//main.MoveReady=false;
varTop=function(){
n=n<v2?n+1:n;
varm=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;};
}
varRight=function(){
n=n<v2?n+1:n;
varm=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;};
}
varBottom=function(){
n=n<v2?n+1:n;
varm=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;};
}
varLeft=function(){
n=n<v2?n+1:n;
varm=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":varP=obj.offsetTop,Point=Top;break;
case"right":varP=obj.offsetLeft,Point=Right;break;
case"bottom":varP=obj.offsetTop,Point=Bottom;break;
case"left":varP=obj.offsetLeft,Point=Left;break;
}
varmyMove=setInterval(Point,5)
}
},
ScrollReady:true,
Scroll:function(s,point){
varobj=this[0],n=0,v=s,main=this,v2=90;
if(this.ScrollReady==true){
//main.ScrollReady=false;
varTop=function(){
n=n<v2?n+1:n;
varm=Sin(n,v);
if(m<Math.abs(s)){obj.scrollTop=P+m}else{obj.scrollTop=P+s;clearInterval(myMove);main.ScrollReady=true;};
}
varRight=function(){
n=n<v2?n+1:n;
varm=Sin(n,v);
if(m<Math.abs(s)){obj.scrollLeft=P-m}else{obj.scrollLeft=P-s;clearInterval(myMove);main.ScrollReady=true;};
}
varBottom=function(){
n=n<v2?n+1:n;
varm=Sin(n,v);
if(m<Math.abs(s)){obj.scrollTop=P-m}else{obj.scrollTop=P-s;clearInterval(myMove);main.ScrollReady=true;};
}
varLeft=function(){
n=n<v2?n+1:n;
varm=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":varP=obj.scrollTop,Point=Top;break;
case"right":varP=obj.scrollLeft,Point=Right;break;
case"bottom":varP=obj.scrollTop,Point=Bottom;break;
case"left":varP=obj.scrollLeft,Point=Left;break;
}
varmyMove=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){
vare=e?e:window.event;
return{x:e.clientX,y:e.clientY}
}
}
HtmlEle.init.prototype=HtmlEle;
</script>
</head>
<body>
<divclass="block"id="moveBlock">
<divclass="moveBlock"></div>
</div>
<scripttype="text/javascript">
varsBlock=newdiv("moveBlock");
document.onclick=function(){
varsite=sBlock.MouseSite(arguments[0]),s=200,b=30;
varobjSite=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程序设计有所帮助。