国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JS實現(xiàn)網(wǎng)頁游戲中滑塊響應鼠標點擊移動效果

2024-05-06 16:24:41
字體:
來源:轉載
供稿:網(wǎng)友

這篇文章主要介紹了JS實現(xiàn)網(wǎng)頁游戲中滑塊響應鼠標點擊移動效果,涉及JavaScript針對頁面鼠標事件、滾動事件及元素屬性等相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了JS實現(xiàn)網(wǎng)頁游戲中滑塊響應鼠標點擊移動效果。分享給大家供大家參考,具體如下:

這是網(wǎng)頁游戲中的一個有趣效果,可以完成以下幾個動作:滾動、scroll 8個方向、鼠標坐標獲娶對象上下左右位置獲取,大家可以自己添加、MoveReady和ScrollReady兩個注釋掉了 取消注釋后防止鼠標連續(xù)點擊效果疊加。

 

具體代碼如下:

 

 
  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>網(wǎng)頁游戲中的一個有趣效果</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. /*返回數(shù)組序號*/ 
  33. Num = function(e, eles) { 
  34. for (var i = 0; i < eles.length; i++) { 
  35. if (eles[i] == e) { return i; } 
  36. /*字符串判定*/ 
  37. GetCur = function(Str, cur) { 
  38. var str = new RegExp(cur, "gi"
  39. return str.test(Str); 
  40. /*DOM構造*/ 
  41. var div=function(id){return new HtmlEle.init(id);} 
  42. HtmlEle={ 
  43. init:function(id){ 
  44. this[0]=id==Object?id:document.getElementById(id); 
  45. return this
  46. }, 
  47. css:function(s){ 
  48. for(var i in s){ 
  49. this[0].style[i]=s[i]; 
  50. }, 
  51. Obj:function(){ 
  52. var obj=this[0]; 
  53. var objTop=obj.offsetTop,objLeft=obj.offsetLeft,objWidth=obj.offsetWidth,objHeight=obj.offsetHeight; 
  54. return {top:objTop,right:objLeft+objWidth,bottom:objTop+objHeight,left:objLeft,width:objWidth,height:objHeight} 
  55. }, 
  56. MoveReady:true
  57. Move:function(s,point){ 
  58. var obj=this[0],n=0,v=s,main=this,v2=90; 
  59. if(this.MoveReady==true){ 
  60. //main.MoveReady=false; 
  61. var Top=function(){ 
  62. n=n<v2?n+1:n; 
  63. var m=Sin(n,v); 
  64. if(m<Math.abs(s)){obj.style.top=P-m+"px"}else{obj.style.top=P-s+"px";clearInterval(myMove);main.MoveReady=true;}; 
  65. var Right=function(){ 
  66. n=n<v2?n+1:n; 
  67. var m=Sin(n,v); 
  68. if(m<Math.abs(s)){obj.style.left=P+m+"px"}else{obj.style.right=P+s+"px";clearInterval(myMove);main.MoveReady=true;}; 
  69. var Bottom=function(){ 
  70. n=n<v2?n+1:n; 
  71. var m=Sin(n,v); 
  72. if(m<Math.abs(s)){obj.style.top=P+m+"px"}else{obj.style.top=P+s+"px";clearInterval(myMove);main.MoveReady=true;}; 
  73. var Left=function(){ 
  74. n=n<v2?n+1:n; 
  75. var m=Sin(n,v); 
  76. if(m<Math.abs(s)){obj.style.left=P-m+"px"}else{obj.style.left=P-s+"px";clearInterval(myMove);main.MoveReady=true;}; 
  77. switch(point){ 
  78. case "top" : var P=obj.offsetTop,Point=Top;break
  79. case "right" : var P=obj.offsetLeft,Point=Right;break
  80. case "bottom" : var P=obj.offsetTop,Point=Bottom;break
  81. case "left" : var P=obj.offsetLeft,Point=Left;break
  82. var myMove=setInterval(Point,5) 
  83. }, 
  84. ScrollReady:true
  85. Scroll:function(s,point){ 
  86. var obj=this[0],n=0,v=s,main=this,v2=90; 
  87. if(this.ScrollReady==true){ 
  88. //main.ScrollReady=false; 
  89. var Top=function(){ 
  90. n=n<v2?n+1:n; 
  91. var m=Sin(n,v); 
  92. if(m<Math.abs(s)){obj.scrollTop=P+m}else{obj.scrollTop=P+s;clearInterval(myMove);main.ScrollReady=true;}; 
  93. var Right=function(){ 
  94. n=n<v2?n+1:n; 
  95. var m=Sin(n,v); 
  96. if(m<Math.abs(s)){obj.scrollLeft=P-m}else{obj.scrollLeft=P-s;clearInterval(myMove);main.ScrollReady=true;}; 
  97. var Bottom=function(){ 
  98. n=n<v2?n+1:n; 
  99. var m=Sin(n,v); 
  100. if(m<Math.abs(s)){obj.scrollTop=P-m}else{obj.scrollTop=P-s;clearInterval(myMove);main.ScrollReady=true;}; 
  101. var Left=function(){ 
  102. n=n<v2?n+1:n; 
  103. var m=Sin(n,v); 
  104. if(m<Math.abs(s)){obj.scrollLeft=P+m}else{obj.scrollLeft=P+s;clearInterval(myMove);main.ScrollReady=true;}; 
  105. switch(point){ 
  106. case "top" : var P=obj.scrollTop,Point=Top;break
  107. case "right" : var P=obj.scrollLeft,Point=Right;break
  108. case "bottom" : var P=obj.scrollTop,Point=Bottom;break
  109. case "left" : var P=obj.scrollLeft,Point=Left;break
  110. var myMove=setInterval(Point,5) 
  111. }, 
  112. Mouse:function(e,f){ 
  113. switch(e){ 
  114. case "click" : this[0].onclick=f;break
  115. case "dbclick" : this[0].ondblclick=f;break
  116. case "over" : this[0].onmouseover=f;break
  117. case "out" : this[0].onmouseout=f;break
  118. }, 
  119. MouseSite:function(e){ 
  120. var e=e?e:window.event; 
  121. return {x:e.clientX,y:e.clientY} 
  122. HtmlEle.init.prototype=HtmlEle; 
  123. </script> 
  124. </head> 
  125. <body> 
  126. <div class="block" id="moveBlock"
  127. <div class="moveBlock"></div> 
  128. </div> 
  129. <script type="text/javascript"
  130. var sBlock=new div("moveBlock"); 
  131. document.onclick=function(){ 
  132. var site=sBlock.MouseSite(arguments[0]),s=200,b=30; 
  133. var objSite=sBlock.Obj(); 
  134. if(objSite.left<site.x && objSite.left+objSite.width/2-b>site.x && objSite.top<site.y &&objSite.bottom>site.y) 
  135. sBlock.Scroll(s,"left"); 
  136. if(objSite.right>site.x && objSite.right-objSite.width/2+b<site.x && objSite.top<site.y &&objSite.bottom>site.y) 
  137. sBlock.Scroll(s,"right"); 
  138. if(objSite.top<site.y && objSite.top+objSite.height/2-b>site.y && objSite.left<site.x && objSite.right>site.x) 
  139. sBlock.Scroll(s,"top"); 
  140. if(objSite.bottom>site.y && objSite.bottom-objSite.height/2+b<site.y) 
  141. sBlock.Scroll(s,"bottom"); 
  142. if(objSite.left>site.x) 
  143. sBlock.Move(s,"left"); 
  144. if(objSite.right<site.x) 
  145. sBlock.Move(s,"right"); 
  146. if(objSite.top>site.y) 
  147. sBlock.Move(s,"top"); 
  148. if(objSite.bottom<site.y) 
  149. sBlock.Move(s,"bottom"); 
  150. window.onload=function(){sBlock[0].scrollTop=0;sBlock[0].scrollLeft=0;} 
  151. </script> 
  152. </body> 
  153. </html> 

希望本文所述對大家JavaScript程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 南部县| 海南省| 吉林市| 玛纳斯县| 鹤岗市| 金昌市| 凌源市| 富民县| 水富县| 平阳县| 海晏县| 青海省| 丰宁| 彝良县| 乌鲁木齐县| 丹巴县| 新野县| 林周县| 简阳市| 资源县| 新密市| 阜新市| 体育| 乡城县| 尚义县| 雅江县| 二手房| 惠水县| 周口市| 中西区| 原平市| 汝阳县| 上饶县| 洛宁县| 平泉县| 汕尾市| 青铜峡市| 兴义市| 巴彦淖尔市| 宜兴市| 台南市|