溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JS+div如何實(shí)現(xiàn)勻速移動(dòng)動(dòng)畫與變速移動(dòng)動(dòng)畫

發(fā)布時(shí)間:2021-08-02 12:23:05 來源:億速云 閱讀:200 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)JS+div如何實(shí)現(xiàn)勻速移動(dòng)動(dòng)畫與變速移動(dòng)動(dòng)畫,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

1.勻速移動(dòng)代碼

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>title</title>
 <style>
  * {
   margin: 0;
   padding: 0;
  }
 
  div {
   margin-top: 20px;
   width: 200px;
   height: 100px;
   background-color: green;
   position: absolute;
   left: 0;
   top: 0;
  }
 </style>
</head>
<body>
<input type="button" value="移動(dòng)到400px" id="btn1"/>
<input type="button" value="移動(dòng)到800px" id="btn2"/>
<div id="dv">
 <script src="common.js"></script>
 <script>
  //點(diǎn)擊按鈕移動(dòng)div
 
  my$("btn1").onclick = function () {
   animate(my$("dv"), 400);
  };
  my$("btn2").onclick = function () {
   animate(my$("dv"), 800);
  };
 
  //勻速動(dòng)畫
  function animate(element, target) {
   //清理定時(shí)器
   clearInterval(element.timeId);
   element.timeId = setInterval(function () {
    //獲取元素的當(dāng)前位置
    var current = element.offsetLeft;
    //移動(dòng)的步數(shù)
    var step = 10;
    step = target > current ? step : -step;
    current += step;
    if (Math.abs(current - target) > Math.abs(step)) {
     element.style.left = current + "px";
    } else {
     clearInterval(element.timeId);
     element.style.left = target + "px";
    }
   }, 20);
  }
 </script>
</div>
</body>
</html>

2.變速移動(dòng)代碼

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>title</title>
 <style>
  * {
   margin: 0;
   padding: 0;
  }
 
  div {
   margin-top: 20px;
   width: 200px;
   height: 100px;
   background-color: green;
   position: absolute;
   left: 0;
   top: 0;
  }
 </style>
</head>
<body>
<input type="button" value="移動(dòng)到400px" id="btn1"/>
<input type="button" value="移動(dòng)到800px" id="btn2"/>
<div id="dv">
 <script src="common.js"></script>
 <script>
  //點(diǎn)擊按鈕移動(dòng)div
 
  my$("btn1").onclick = function () {
   animate(my$("dv"), 400);
  };
  my$("btn2").onclick = function () {
   animate(my$("dv"), 800);
  };
 
  //變速動(dòng)畫
  function animate(element, target) {
   //清理定時(shí)器
   clearInterval(element.timeId);
   element.timeId = setInterval(function () {
    //獲取元素的當(dāng)前位置
    var current = element.offsetLeft;
    //移動(dòng)的步數(shù)
    var step = (target-current)/10;
    step = step>0?Math.ceil(step):Math.floor(step);
    current += step;
    element.style.left = current + "px";
    if(current==target) {
     //清理定時(shí)器
     clearInterval(element.timeId);
    }
    
   }, 20);
  }
 </script>
</div>
</body>
</html>

common.js

/**
 * 獲取指定標(biāo)簽對(duì)象
 * @param id 標(biāo)簽的id屬性值
 * @returns {Element}根據(jù)id屬性值返回指定標(biāo)簽對(duì)象
 */
function my$(id) {
  return document.getElementById(id);
}

關(guān)于“JS+div如何實(shí)現(xiàn)勻速移動(dòng)動(dòng)畫與變速移動(dòng)動(dòng)畫”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI