幽灵资源网 Design By www.bzswh.com

本文实例为大家分享了js实现盒子移动动画效果的具体代码,供大家参考,具体内容如下

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport"
   content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Document</title>
 <style>
  .box {
   width: 200px;
   height: 200px;
   border: 1px solid red;
   position: absolute;
   left: 0;
   top: 50px;
  }
 </style>
</head>
<body>
<input type="button" value="前进" id="box_start">
<input type="button" value="停止" id="box_stop">
<input type="button" value="回退" id="box_back">
<br><br>
<div id="box" class="box">

</div>
<script>
 let boxStart = document.getElementById("box_start");
 let boxStop = document.getElementById("box_stop");
 let boxBack = document.getElementById("box_back");
 let timeId_1;
 let timeId_2;

 boxStart.onclick = function () {
  let box = document.getElementById("box");
  clearInterval(timeId_2);
  timeId_1 = setInterval(function () {
   if (box.offsetLeft >= 600) {
    clearInterval(timeId_1);
    box.style.left = 600 + 'px';
    alert('到达目的地');
   } else {
    box.style.left = box.offsetLeft + 10 + 'px';
   }
  }, 100);
 };
 boxBack.onclick = function () {
  let box = document.getElementById("box");
  clearInterval(timeId_1);
  timeId_2 = setInterval(function () {
   if (box.offsetLeft <= 0) {
    clearInterval(timeId_2);
    box.style.left = "0";
    alert('已在出发位置');
   } else {
    box.style.left = box.offsetLeft - 10 + 'px';
   }
  }, 100);
 };
 boxStop.onclick = function () {
  clearInterval(timeId_1);
  clearInterval(timeId_2);
 };
</script>
</body>
</html>

效果图:

js实现盒子移动动画效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
js,盒子移动

幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com