JS實(shí)現(xiàn)網(wǎng)頁游戲中滑塊響應(yīng)鼠標(biāo)點(diǎn)擊移動(dòng)效果
本文實(shí)例講述了JS實(shí)現(xiàn)網(wǎng)頁游戲中滑塊響應(yīng)鼠標(biāo)點(diǎn)擊移動(dòng)效果。分享給大家供大家參考,具體如下:
這是網(wǎng)頁游戲中的一個(gè)有趣效果,可以完成以下幾個(gè)動(dòng)作:滾動(dòng)、scroll 8個(gè)方向、鼠標(biāo)坐標(biāo)獲娶對象上下左右位置獲取,大家可以自己添加、MoveReady和ScrollReady兩個(gè)注釋掉了 取消注釋后防止鼠標(biāo)連續(xù)點(diǎn)擊效果疊加。
在線演示地址如下:
http://demo.jb51.net/js/2015/js-web-game-click-move-demo/
具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>網(wǎng)頁游戲中的一個(gè)有趣效果</title> <style type="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:3px solid #fdd;overflow:hidden;} .moveBlock{width:1900px;height:2728px;background:url("http://img.jbzj.com/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; } /*返回?cái)?shù)組序號(hào)*/ Num = function(e, eles) { for (var i = 0; i < eles.length; i++) { if (eles[i] == e) { return i; } } } /*字符串判定*/ GetCur = function(Str, cur) { var str = new RegExp(cur, "gi") return str.test(Str); } /*DOM構(gòu)造*/ 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){ //main.MoveReady=false; 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){ //main.ScrollReady=false; 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程序設(shè)計(jì)有所幫助。
- Vue 實(shí)現(xiàn)拖動(dòng)滑塊驗(yàn)證功能(只有css+js沒有后臺(tái)驗(yàn)證步驟)
- 基于JS組件實(shí)現(xiàn)拖動(dòng)滑塊驗(yàn)證功能(代碼分享)
- 基于JavaScript實(shí)現(xiàn)拖動(dòng)滑塊效果
- Javascript實(shí)現(xiàn)滑塊滑動(dòng)改變值的實(shí)現(xiàn)代碼
- js實(shí)現(xiàn)兼容PC端和移動(dòng)端滑塊拖動(dòng)選擇數(shù)字效果
- JS響應(yīng)鼠標(biāo)點(diǎn)擊實(shí)現(xiàn)兩個(gè)滑塊區(qū)間拖動(dòng)效果
- js用拖動(dòng)滑塊來控制圖片大小的方法
- 基于Vue.js實(shí)現(xiàn)tab滑塊效果
- Javascript 鼠標(biāo)移動(dòng)上去小三角形滑塊緩慢跟隨效果
- 原生js實(shí)現(xiàn)滑塊區(qū)間組件
相關(guān)文章
js獲取url中的參數(shù)且參數(shù)為中文時(shí)通過js解碼
這篇文章主要介紹了url中傳遞中文參數(shù)的時(shí)候通過js解碼,需要的朋友可以參考下2014-03-03簡單js代碼實(shí)現(xiàn)selece二級(jí)聯(lián)動(dòng)(推薦)
這篇文章主要介紹了簡單js代碼實(shí)現(xiàn)selece二級(jí)聯(lián)動(dòng)的簡單實(shí)例。需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02js實(shí)現(xiàn)編輯div節(jié)點(diǎn)名稱的方法
這篇文章主要介紹了js實(shí)現(xiàn)編輯div節(jié)點(diǎn)名稱的方法,可實(shí)現(xiàn)針對div節(jié)點(diǎn)名稱的編輯及樣式的選擇效果,并且分別針對IE與FF瀏覽器的樣式進(jìn)行了選擇與控制,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12使用javascript實(shí)現(xiàn)判斷當(dāng)前瀏覽器
這篇文章主要介紹了使用javascript實(shí)現(xiàn)判斷當(dāng)前瀏覽器的類型及版本,雖然不是很全面,但是還是推薦給大家,簡單學(xué)下方法和思路。2015-04-04javascript中json對象json數(shù)組json字符串互轉(zhuǎn)及取值方法
這篇文章主要介紹了javascript中json對象json數(shù)組json字符串互轉(zhuǎn)及取值方法,需要的朋友可以參考下2017-04-04js獲取location.href的參數(shù)實(shí)例代碼
本文為大家介紹下js如何獲取location.href的參數(shù),需要注意的是去掉參數(shù)里最開頭的?號(hào),具體實(shí)現(xiàn)如下,有需要的朋友可以參考下,希望對大家有所幫助2013-08-08JavaScript實(shí)現(xiàn)Tab標(biāo)簽頁切換的最簡便方式(4種)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)Tab標(biāo)簽頁切換的最簡便方式(4種),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-0622種JavaScript中數(shù)組常用API總結(jié)
在前端開發(fā)中,數(shù)組是一種常見且重要的數(shù)據(jù)結(jié)構(gòu),本文主要介紹了前端中數(shù)組常用的API,包括添加、刪除、截取、合并、轉(zhuǎn)換等操作,希望對大家有所幫助2023-05-05