国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript反彈動畫效果的實現代碼

2019-11-19 16:05:11
字體:
來源:轉載
供稿:網友

代碼如下:

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Document</title>  <style>    #box{      width:200px;      height:200px;      position: absolute;      top:0;      left:200px;      background:lightblue;    }    .btn{      position:absolute;      top:200px;      left:100px;      height:50px;    }    .btn input{      display:inline-block;      margin-left:50px;      outline: none;      width:100px;      height:50px;      border:1px solid green;      cursor:pointer;    }  </style></head><body>  <div id='box'></div>  <div class='btn'>    <input type="button" value='向左' id='btnLeft'>    <input type="button" value='向右' id='btnRight'>  </div>  <script>    var oBox = document.getElementById("box");    var minLeft = 0;    var maxLeft = utils.win('clientWidth')-oBox.offsetWidth;    var step = 5;    var timer = null;    function move(target){      //target:告訴我要運動的目標位置      window.clearTimeout(timer);      var curLeft = utils.css(oBox,"left");      if(curLeft<target){//向右走        if(curLeft+step>target){//邊界          utils.css(oBox,"left",target);          return;        }        curLeft+=step;        utils.css(oBox,"left",curLeft)      }else if(curLeft>target){//向左走        if(curLeft-step<target){//邊界          utils.css(oBox,"left",target);          return;        }        curLeft-=step;        utils.css(oBox,"left",curLeft)      }else{//不需要運動        return;      }      // timer = window.setTimeout(move,10)//這里有一個問題,點擊按鈕第一次target的值是有的,但是第二次通過setTimeout執行的時候沒有給target進行傳值。是undefined      timer = window.setTimeout(function(){        move(target);      },10)//這樣使用匿名函數包裹一下,就解決了上面的問題,但是這樣寫性能不好,因為每一次到達時間的時候,都需要執行一次匿名函數(形成一個私有的作用域),在匿名函數中再執行move,但是move中需要用到的數據值在第一次執行的move方法中,需要把匿名函數形成的這個私有的作用域作為跳板找到之前的,這樣就導致了匿名函數形成的這個私有的作用域不能銷毀    }    document.getElementById('btnLeft').onclick = function(){      move(minLeft)    }    document.getElementById('btnRight').onclick = function(){      move(maxLeft)    }  </script></body></html>

為了解決上面性能不好的問題,下面是一個優化后的代碼:里面在使用一個函數包裹,這樣就只有move函數創建的一個私有作用域沒有銷毀,等到_move執行完畢,move就自然會進行銷毀。

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Document</title>  <style>    #box{      width:200px;      height:200px;      position: absolute;      top:0;      left:200px;      background:lightblue;    }    .btn{      position:absolute;      top:200px;      left:100px;      height:50px;    }    .btn input{      display:inline-block;      margin-left:50px;      outline: none;      width:100px;      height:50px;      border:1px solid green;      cursor:pointer;    }  </style></head><body>  <div id='box'></div>  <div class='btn'>    <input type="button" value='向左' id='btnLeft'>    <input type="button" value='向右' id='btnRight'>  </div>  <script>    var oBox = document.getElementById("box");    var minLeft = 0;    var maxLeft = utils.win('clientWidth')-oBox.offsetWidth;    var step = 5;    var timer = null;    function move(target){      //target:告訴我要運動的目標位置      _move();      function _move(){        window.clearTimeout(timer);        var curLeft = utils.css(oBox,"left");        if(curLeft<target){//向右走          if(curLeft+step>target){//邊界            utils.css(oBox,"left",target);            return;          }          curLeft+=step;          utils.css(oBox,"left",curLeft)        }else if(curLeft>target){//向左走          if(curLeft-step<target){//邊界            utils.css(oBox,"left",target);            return;          }          curLeft-=step;          utils.css(oBox,"left",curLeft)        }else{//不需要運動          return;        }        timer = window.setTimeout(_move,10);      }    }    document.getElementById('btnLeft').onclick = function(){      move(minLeft)    }    document.getElementById('btnRight').onclick = function(){      move(maxLeft)    }  </script></body></html>

注意:為了讓當前的元素在同一時間只運行一個動畫(下一個動畫開始的時候首先把上一個動畫的定時器清除掉):保證當前元素所有動畫接收定時器返回值的那個變量需要共享,有兩種方式:1、全局接收(例如上面的代碼 var timer = null)2、給元素增加自定義屬性(如下圖所示)

總結:通過以上可以得出動畫優化的四條規則:

  1、邊界判斷加步長

  2、清除沒有用的定時器

  3、在外層函數需要傳參的時候,可以在里面在嵌套一層函數,避免作用域的累積。

  4、把定時器的返回值存儲在元素的自定義屬性上,防止全局變量沖突和同一時間多個動畫執行

以上所述是小編給大家介紹的JavaScript反彈動畫效果的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 眉山市| 错那县| 宝丰县| 阜新| 三明市| 威远县| 康乐县| 麻城市| 电白县| 平果县| 吴江市| 广州市| 礼泉县| 昌图县| 东乡县| 称多县| 峨眉山市| 德格县| 泊头市| 天祝| 合作市| 内黄县| 阿鲁科尔沁旗| 柯坪县| 日土县| 新建县| 永川市| 英德市| 黄山市| 陆良县| 扶风县| 沧源| 盐城市| 上犹县| 扶沟县| 松阳县| 名山县| 梅河口市| 买车| 合肥市| 辽阳县|