首页 > 语言 > JavaScript > 正文

JS实现网页游戏中滑块响应鼠标点击移动效果

2024-05-06 16:24:41
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了JS实现网页游戏中滑块响应鼠标点击移动效果,涉及JavaScript针对页面鼠标事件、滚动事件及元素属性等相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了JS实现网页游戏中滑块响应鼠标点击移动效果。分享给大家供大家参考,具体如下:

这是网页游戏中的一个有趣效果,可以完成以下几个动作:滚动、scroll 8个方向、鼠标坐标获娶对象上下左右位置获取,大家可以自己添加、MoveReady和ScrollReady两个注释掉了 取消注释后防止鼠标连续点击效果叠加。

 

具体代码如下:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"><head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>网页游戏中的一个有趣效果</title> 
  6. <style type="text/css"> 
  7. 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;} 
  8. body{font:12px/24px "宋体";background:#fff;color:#333;} 
  9. ol, ul{list-style:none;} 
  10. blockquote, q {quotes:none;} 
  11. blockquote:before, blockquote:after, 
  12. q:before, q:after {content: '';content:none;} 
  13. :focus {outline:0;} 
  14. ins {text-decoration:none;} 
  15. del {text-decoration:line-through;} 
  16. table {border-collapse:collapse;border-spacing:0;} 
  17. label{cursor:pointer} 
  18. input[type="button"]{cursor:pointer} 
  19. button{cursor:pointer} 
  20. strong{font-weight:normal;} 
  21. em{font-style:normal} 
  22. a{text-decoration:none;color:#f60} 
  23. a:hover{text-decoration:underline;} 
  24. .block{width:200px;height:200px;background:#e63;position:absolute;left:50%;top:50%;border:3px solid #fdd;overflow:hidden;} 
  25. .moveBlock{width:1900px;height:2728px;background:url("http://files.vevb.com/file_images/article/201510/2015101993654799.jpg") left top;} 
  26. </style> 
  27. <script type="text/javascript"> 
  28. var ie = ! -[1, ], myDate = new Date(); 
  29. function Sin(x,v) { 
  30. var y = Math.round(Math.sin(Math.PI*x/180)*100)/100; 
  31. return ie?y*v*3:y*v; 
  32. } 
  33. /*返回数组序号*/ 
  34. Num = function(e, eles) { 
  35. for (var i = 0; i < eles.length; i++) { 
  36. if (eles[i] == e) { return i; } 
  37. } 
  38. } 
  39. /*字符串判定*/ 
  40. GetCur = function(Str, cur) { 
  41. var str = new RegExp(cur, "gi") 
  42. return str.test(Str); 
  43. } 
  44. /*DOM构造*/ 
  45. var div=function(id){return new HtmlEle.init(id);} 
  46. HtmlEle={ 
  47. init:function(id){ 
  48. this[0]=id==Object?id:document.getElementById(id); 
  49. return this; 
  50. }, 
  51. css:function(s){ 
  52. for(var i in s){ 
  53. this[0].style[i]=s[i]; 
  54. } 
  55. }, 
  56. Obj:function(){ 
  57. var obj=this[0]; 
  58. var objTop=obj.offsetTop,objLeft=obj.offsetLeft,objWidth=obj.offsetWidth,objHeight=obj.offsetHeight; 
  59. return {top:objTop,right:objLeft+objWidth,bottom:objTop+objHeight,left:objLeft,width:objWidth,height:objHeight} 
  60. }, 
  61. MoveReady:true, 
  62. Move:function(s,point){ 
  63. var obj=this[0],n=0,v=s,main=this,v2=90; 
  64. if(this.MoveReady==true){ 
  65. //main.MoveReady=false; 
  66. var Top=function(){ 
  67. n=n<v2?n+1:n; 
  68. var m=Sin(n,v); 
  69. if(m<Math.abs(s)){obj.style.top=P-m+"px"}else{obj.style.top=P-s+"px";clearInterval(myMove);main.MoveReady=true;}; 
  70. } 
  71. var Right=function(){ 
  72. n=n<v2?n+1:n; 
  73. var m=Sin(n,v); 
  74. if(m<Math.abs(s)){obj.style.left=P+m+"px"}else{obj.style.right=P+s+"px";clearInterval(myMove);main.MoveReady=true;}; 
  75. } 
  76. var Bottom=function(){ 
  77. n=n<v2?n+1:n; 
  78. var m=Sin(n,v); 
  79. if(m<Math.abs(s)){obj.style.top=P+m+"px"}else{obj.style.top=P+s+"px";clearInterval(myMove);main.MoveReady=true;}; 
  80. } 
  81. var Left=function(){ 
  82. n=n<v2?n+1:n; 
  83. var m=Sin(n,v); 
  84. if(m<Math.abs(s)){obj.style.left=P-m+"px"}else{obj.style.left=P-s+"px";clearInterval(myMove);main.MoveReady=true;}; 
  85. } 
  86. switch(point){ 
  87. case "top" : var P=obj.offsetTop,Point=Top;break; 
  88. case "right" : var P=obj.offsetLeft,Point=Right;break; 
  89. case "bottom" : var P=obj.offsetTop,Point=Bottom;break; 
  90. case "left" : var P=obj.offsetLeft,Point=Left;break; 
  91. } 
  92. var myMove=setInterval(Point,5) 
  93. } 
  94. }, 
  95. ScrollReady:true, 
  96. Scroll:function(s,point){ 
  97. var obj=this[0],n=0,v=s,main=this,v2=90; 
  98. if(this.ScrollReady==true){ 
  99. //main.ScrollReady=false; 
  100. var Top=function(){ 
  101. n=n<v2?n+1:n; 
  102. var m=Sin(n,v); 
  103. if(m<Math.abs(s)){obj.scrollTop=P+m}else{obj.scrollTop=P+s;clearInterval(myMove);main.ScrollReady=true;}; 
  104. } 
  105. var Right=function(){ 
  106. n=n<v2?n+1:n; 
  107. var m=Sin(n,v); 
  108. if(m<Math.abs(s)){obj.scrollLeft=P-m}else{obj.scrollLeft=P-s;clearInterval(myMove);main.ScrollReady=true;}; 
  109. } 
  110. var Bottom=function(){ 
  111. n=n<v2?n+1:n; 
  112. var m=Sin(n,v); 
  113. if(m<Math.abs(s)){obj.scrollTop=P-m}else{obj.scrollTop=P-s;clearInterval(myMove);main.ScrollReady=true;}; 
  114. } 
  115. var Left=function(){ 
  116. n=n<v2?n+1:n; 
  117. var m=Sin(n,v); 
  118. if(m<Math.abs(s)){obj.scrollLeft=P+m}else{obj.scrollLeft=P+s;clearInterval(myMove);main.ScrollReady=true;}; 
  119. } 
  120. switch(point){ 
  121. case "top" : var P=obj.scrollTop,Point=Top;break; 
  122. case "right" : var P=obj.scrollLeft,Point=Right;break; 
  123. case "bottom" : var P=obj.scrollTop,Point=Bottom;break; 
  124. case "left" : var P=obj.scrollLeft,Point=Left;break; 
  125. } 
  126. var myMove=setInterval(Point,5) 
  127. } 
  128. }, 
  129. Mouse:function(e,f){ 
  130. switch(e){ 
  131. case "click" : this[0].onclick=f;break; 
  132. case "dbclick" : this[0].ondblclick=f;break; 
  133. case "over" : this[0].onmouseover=f;break; 
  134. case "out" : this[0].onmouseout=f;break; 
  135. } 
  136. }, 
  137. MouseSite:function(e){ 
  138. var e=e?e:window.event; 
  139. return {x:e.clientX,y:e.clientY} 
  140. } 
  141. } 
  142. HtmlEle.init.prototype=HtmlEle; 
  143. </script> 
  144. </head> 
  145. <body> 
  146. <div class="block" id="moveBlock"> 
  147. <div class="moveBlock"></div> 
  148. </div> 
  149. <script type="text/javascript"> 
  150. var sBlock=new div("moveBlock"); 
  151. document.onclick=function(){ 
  152. var site=sBlock.MouseSite(arguments[0]),s=200,b=30; 
  153. var objSite=sBlock.Obj(); 
  154. if(objSite.left<site.x && objSite.left+objSite.width/2-b>site.x && objSite.top<site.y &&objSite.bottom>site.y) 
  155. sBlock.Scroll(s,"left"); 
  156. if(objSite.right>site.x && objSite.right-objSite.width/2+b<site.x && objSite.top<site.y &&objSite.bottom>site.y) 
  157. sBlock.Scroll(s,"right"); 
  158. if(objSite.top<site.y && objSite.top+objSite.height/2-b>site.y && objSite.left<site.x && objSite.right>site.x) 
  159. sBlock.Scroll(s,"top"); 
  160. if(objSite.bottom>site.y && objSite.bottom-objSite.height/2+b<site.y) 
  161. sBlock.Scroll(s,"bottom"); 
  162. if(objSite.left>site.x) 
  163. sBlock.Move(s,"left"); 
  164. if(objSite.right<site.x) 
  165. sBlock.Move(s,"right"); 
  166. if(objSite.top>site.y) 
  167. sBlock.Move(s,"top"); 
  168. if(objSite.bottom<site.y) 
  169. sBlock.Move(s,"bottom"); 
  170. } 
  171. window.onload=function(){sBlock[0].scrollTop=0;sBlock[0].scrollLeft=0;} 
  172. </script> 
  173. </body> 
  174. </html> 

希望本文所述对大家JavaScript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选