我的编程空间,编程开发者的网络收藏夹
学习永远不晚

这么用原生js操作dom实现上下左右移动

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

这么用原生js操作dom实现上下左右移动

这篇文章主要介绍“这么用原生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()

         }

}

 

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实现上下左右移动”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

这么用原生js操作dom实现上下左右移动

下载Word文档到电脑,方便收藏和打印~

下载Word文档

编程热搜

目录