亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

這么用原生js操作dom實現上下左右移動

發布時間:2022-03-14 17:17:09 來源:億速云 閱讀:567 作者:iii 欄目:web開發

這篇文章主要介紹“這么用原生js操作dom實現上下左右移動”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“這么用原生js操作dom實現上下左右移動”文章能幫助大家解決問題。

代碼如下:

<!DOCTYPE html>

<html lang="en">

<head>

         <meta charset="UTF-8">

         <title>Document</title>

         <style>

         *{

                   padding: 0;

                   margin:0;

         }

 

         html,body,main{

                   height: 100%;

                   width: 100%;

         }

         .box{

                   height: 20px;

                   width: 20px;

                   border-radius: 50%;

                   background:red;

                   position: absolute;

                   left: 0;

                   right: 0;

         }

         .lookFood{

                   font-size: 20px;

                   position: absolute;

                   left: 50%;

                   top:40px;

                   transform: translateX(-50%);

         }

         .food{

                   height: 20px;

                   width: 20px;

                   background: #faa;

                   border-radius: 50%;

                   position: absolute;

         }

 

         footer span{

                   position: absolute;

                   bottom: 20px;

                   left: 50%;

                   transform: translateX(-50%);

         }

         </style>

 

</head>

<body>

<main>

         <div class="lookFood">獲取了<span>0</span>food</div>

         <div class="box"></div>

         <div class="food"></div>

</main>  

<footer>

         <span>小提示:按下&uarr;&darr;&larr;&rarr;方向鍵開始收集食物吧</span>

</footer>

</body>

 

<script>

var box = document.getElementsByClassName("box")[0],

         food = document.getElementsByClassName("food")[0],

         span = document.getElementsByTagName("span")[0],

         //獲取的食物個數

         i = 0;

//初始化food的位置;每次吃到調用

function foodInit() {

         food.style.left = Math.random() * parseInt(window.getComputedStyle(document.body).width) - 20 + "px";

         food.style.top = Math.random() * parseInt(window.getComputedStyle(document.body).height) - 20 + "px";

         //獲取隨機的16進制顏色值;

         food.style.background = '#' + (~~(Math.random() * (1 << 24))).toString(16);

}

//小球每次運動調用這個函數 判斷有沒有找到食物;

function start() {

         var boxLeft = parseInt(window.getComputedStyle(box).left),

                   boxTop = parseInt(window.getComputedStyle(box).top),

                   foodLeft = parseInt(window.getComputedStyle(food).left),

                   foodTop = parseInt(window.getComputedStyle(food).top);

         //console.log(Math.abs(boxLeft - foodLeft) + "left+" + Math.abs(boxTop - foodTop) + "top+" + parseInt(window.getComputedStyle(food).height))

         if (Math.abs(boxLeft - foodLeft) < parseInt(window.getComputedStyle(food).width) && Math.abs(boxTop - foodTop) < parseInt(window.getComputedStyle(food).height)) {

                   span.innerHTML = ++i;

                   foodInit()

         }

}

 

/**

 *鍵盤上下左右觸發dom移動;

 *可以同時觸發兩個方向的事件;

 *傳入四個參數:dom 需要移動的dom;

 *main 移動的范圍容器

 *speed 每秒移動速度;

 *callback 每次執行觸發的函數;

 */

var keyDomMove = (function() {

         //通過閉包保存變量

         var keyCode = {

                   //每次按下上下左右的將當前按下的方向保存 ture;

                   downKeyCode: function(e) {

                            var e = e || window.event;

                            //只需要用到上下左右 只保存4個鍵值;

                            if (e.keyCode === 37 || e.keyCode === 38 || e.keyCode === 39 || e.keyCode === 40) {

                                     keyCode[e.keyCode] = true;

                            }

                   },

                   //每次彈起上下左右的將當前彈起的方向修改為flase;

                   upKeyCode: function(e) {

                            var e = e || window.event;

                            if (keyCode[e.keyCode]) {

                                     keyCode[e.keyCode] = false;

                            }

                   },

                   //用于保存當前的setInterval;通過定時器增強小球移動的連貫性;

                   time: {},

         };

         return function(dom, main, speed,callback) {

                   if (typeof speed != "number") {

                            speed = 1;

                   } else {

                            //速度必須大于60px每秒;每次移動的像素小于1px 瀏覽器會修正為0px;這也是因為運用了定時器的缺點;

                            //除以60是因為瀏覽器播放動畫每秒60幀可以保持動畫的流暢性;

                            speed = (speed/60) > 1 ? (speed/60) : 1

                   }

                   var left = parseInt(window.getComputedStyle(dom).marginLeft),

                            top = parseInt(window.getComputedStyle(dom).marginTop),

                            mainHeigeht=parseInt(window.getComputedStyle(main).height)-20,

                            mainWidth=parseInt(window.getComputedStyle(main).width)-20,

                            //用于左右 和上下穿透;

                            changeXY=function(xy,min,max){

                                     if(xy<min){

                                               xy=max;

                                     }else if(xy>=max){

                                               xy=min;

                                     }

                                     return xy;

                            };

 

                   document.addEventListener("keydown", function(e) {

                        e.preventDefault()

                            keyCode.downKeyCode();

                            if (keyCode[37] && !keyCode.time[37]) {

                                     keyCode.time[e.keyCode] = setInterval(function() {

                                               var x= dom.offsetLeft - left - speed ;

                                               x=changeXY(x,0,mainWidth);

                                               dom.style.left =x+"px";

                                               callback&&callback();

                                     }, 50/3)

 

                            };

                            if (keyCode[38] && !keyCode.time[38]) {

                                     keyCode.time[e.keyCode] = setInterval(function() {

                                               var y = dom.offsetTop - top - speed

                                               y=changeXY(y,0,mainHeigeht);

                                               dom.style.top=y+"px";

                                               callback&&callback();

                                     }, 50/3)

 

                            };

                            if (keyCode[39] && !keyCode.time[39]) {

                                     keyCode.time[e.keyCode] = setInterval(function() {

                                               var x= dom.offsetLeft - left + speed ;

                                               x=changeXY(x,0,mainWidth);

                                               dom.style.left =x+"px";

                                               callback&&callback();

                                     }, 50/3)

 

                            };

                            if (keyCode[40] && !keyCode.time[40]) {

                                     keyCode.time[e.keyCode] = setInterval(function() {

                                               var y = dom.offsetTop - top + speed ;

                                               y=changeXY(y,0,mainHeigeht);

                                               dom.style.top=y+"px";

                                               callback&&callback();

                                     }, 50/3)

 

                            };

                            //每次彈起按鍵 移除當前方向的定時器 ;

                            document.addEventListener("keyup", function(e) {

                                     keyCode.upKeyCode();

                                     clearInterval(keyCode.time[e.keyCode]);

                                     keyCode.time[e.keyCode] = null;

                            })

                   })

 

         }

 

})()

 

function init() {

         foodInit(food);

         keyDomMove(box, document.body,500,start);

}

init()

</script>

</html>

關于“這么用原生js操作dom實現上下左右移動”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

闽侯县| 台中市| 涞水县| 济宁市| 略阳县| 晴隆县| 岚皋县| 盈江县| 宾川县| 海伦市| 泽普县| 河源市| 板桥市| 桦川县| 兴宁市| 张家川| 安塞县| 湖南省| 白水县| 卓资县| 杨浦区| 江山市| 金门县| 东阳市| 通化市| 扎兰屯市| 梁山县| 梅州市| 东阿县| 南阳市| 黄浦区| 九寨沟县| 凤冈县| 德兴市| 扎鲁特旗| 固安县| 康定县| 罗源县| 双峰县| 城市| 上饶市|