匀速直线运动

 物体直线运动(从左向右运动):

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>温故而知新</title>
  <style>
    div {
      width: 100px;
      height: 100px;
      background-color: #ff0000;
      position: absolute;
      left: 0px;
      top: 0px;
    }

    span {
      position: absolute;
      width: 1px;
      height: 100px;
      background-color: #333333;
      top: 0;
      left: 300px;
    }

    button {
      margin-top: 200px;
    }
  </style>
</head>

<body>
  <div></div>
  <span></span>
  <button>点击移动方格</button>
</body>
<script>
  /*
    物体直线运动(从左向右运动)
  */
  let div = document.getElementsByTagName("div")[0];
  const button = document.getElementsByTagName("button")[0];
  let moveDiv = null;
  button.onclick = function () {
    clearInterval(moveDiv)
    const moveDistance = 7;
    moveDiv = setInterval(function () {
      if (300 - div.offsetLeft < moveDistance) {
        div.style.left = '300px';
        clearInterval(moveDiv)
      } else {
        div.style.left = div.offsetLeft + moveDistance + 'px';
      }
    }, 70)
  }
</script>

</html>

 

 物体直线运动(从右向左运动):

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>温故而知新</title>
  <style>
    div {
      width: 100px;
      height: 100px;
      background-color: #ff0000;
      position: absolute;
      left: 600px;
      top: 0px;
    }

    span {
      position: absolute;
      width: 1px;
      height: 100px;
      background-color: #333333;
      top: 0;
      left: 300px;
    }

    button {
      margin-top: 200px;
    }
  </style>
</head>

<body>
  <div></div>
  <span></span>
  <button>点击移动方格</button>
</body>
<script>
  /*
    物体直线运动(从右向左运动)
  */
  let div = document.getElementsByTagName("div")[0];
  const button = document.getElementsByTagName("button")[0];
  let moveDiv = null;
  button.onclick = function () {
    clearInterval(moveDiv)
    const moveDistance = 300 - div.offsetLeft > 0 ? 7 : -7;
    moveDiv = setInterval(function () {
      if (Math.abs(300 - div.offsetLeft) < Math.abs(moveDistance)) {
        div.style.left = '300px';
        clearInterval(moveDiv)
      } else {
        div.style.left = div.offsetLeft + moveDistance + 'px';
      }
    }, 70)
  }
</script>

</html>

 


物体直线运动(将代码封装成为starMove方法去调用,并且为了让这个方法更灵活,将指定三个参数:

    第一个参数是指定移动的元素elem;

    第二个参数是每次移动的距离distance;

    第三个参数是移动到哪个位置;

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>温故而知新</title>
  <style>
    div {
      width: 100px;
      height: 100px;
      background-color: #ff0000;
      position: absolute;
      left: 0px;
      top: 0px;
    }

    span {
      position: absolute;
      width: 1px;
      height: 100px;
      background-color: #333333;
      top: 0;
      left: 300px;
    }

    button {
      margin-top: 200px;
    }
  </style>
</head>

<body>
  <div></div>
  <span></span>
  <button>点击移动方格</button>
</body>
<script>
  /*
    物体直线运动(将代码封装成为starMove方法去调用,并且为了让这个方法更灵活,
    将指定两个参数:
    第一个参数是指定移动的元素elem;
    第二个参数是每次移动的距离distance;
    第三个参数是移动到哪个位置;
    )
  */
  let div = document.getElementsByTagName("div")[0];
  const button = document.getElementsByTagName("button")[0];
  button.onclick = function () {
    starMove(div, 7, 300)
  }

 function starMove(elem, distance, site) {
    let moveDiv = null;
    clearInterval(moveDiv)
    const moveDistance = site - elem.offsetLeft > 0 ? distance : -distance;
    moveDiv = setInterval(function () {
      if (Math.abs(site - elem.offsetLeft) < Math.abs(moveDistance)) {
        elem.style.left = site + "px";
        clearInterval(moveDiv)
      } else {
        elem.style.left = elem.offsetLeft + moveDistance + 'px';
      }
    }, 70)
  }
</script>

</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值