欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript定時(shí)器的簡(jiǎn)單應(yīng)用示例【控制方塊移動(dòng)】

 更新時(shí)間:2019年06月17日 10:07:19   作者:longzhoufeng  
這篇文章主要介紹了javascript定時(shí)器的簡(jiǎn)單應(yīng)用,結(jié)合javascript事件觸發(fā)控制方塊移動(dòng)操作分析了javascript定時(shí)器使用相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了javascript定時(shí)器的簡(jiǎn)單應(yīng)用。分享給大家供大家參考,具體如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>www.dbjr.com.cn 定時(shí)器的應(yīng)用</title>
  <style>
    #Div1 { width: 100px; height: 100px; position: absolute; background-color: red; top: 50px; }
  </style>
</head>
<body>
<input id="oBtn1" type="button" value="銨鈕">
<div id="Div1"></div>
<script>
  var oBtn = document.getElementById("oBtn1");
  var oDiv = document.getElementById("Div1");
  //var timer=null;
  //oDiv.timer=null;
  oBtn.onclick = function() {
    clearInterval(oDiv.timer)
    oDiv.timer = setInterval(function() {
      var speed = parseInt(getStyle(oDiv, "left")) + 9;
      if(speed > 800) {
        speed = 800;
      }
      oDiv.style.left = speed + "px"
      if(speed == 800) {
        clearInterval(oDiv.timer);
        //alert(speed)
      }
    }, 50)
  }
  function getStyle(obj, attr) {
    return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr]
  }
</script>
</body>
</html>

運(yùn)行結(jié)果:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論