zoukankan      html  css  js  c++  java
  • promise实例小球运动

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Document</title>
     <script src="http://apps.bdimg.com/libs/bluebird/1.2.2/bluebird.js"></script>
    <style>
    *{margin:0; padding:0;}
    .div1{50px; height:50px; background:red; border-radius:50%; margin-top:10px; margin-left:0;}
    .div2{50px; height:50px; background:yellow; border-radius:50%; margin-top:10px; margin-left:0;}
    .div3{50px; height:50px; background:blue; border-radius:50%; margin-top:10px; margin-left:0;}
    </style>
    </head>
    
    <body>
    <div style="margin:20px; position:relative">
    <div class="div1"></div>
    <div class="div2"></div>
    <div class="div3"></div>
    </div>
    <script>
    window.onload = function(){
    /*function move(obj, dis, cb){
      
       setTimeout(function(){
          var marginLeft = parseInt(obj.offsetLeft,10);
          if(marginLeft == dis) {
             cb && cb()
          } else {
             if(marginLeft < dis) {
               marginLeft++
             } else {
               marginLeft--
             }
             obj.style.marginLeft = marginLeft + 'px' 
             move(obj, dis, cb)
          }
        },10)
       
    }
    move(d1,150,function(){
      move(d2,150,function(){
        move(d3, 150, function(){
          move(d3,0, function() {
            move(d2,0,function(){
              move(d1,0)
            })
          })
        })
      })
    })*/
    var d1 = document.querySelector('.div1')
    var d2 = document.querySelector('.div2')
    var d3 = document.querySelector('.div3')
    var Promise = window.Promise
    function promiseMove(obj,dis) {
      return new Promise(function(resolve, reject) {
        function move(){
           setTimeout(function(){
              var marginLeft = parseInt(obj.offsetLeft,10);
              if(marginLeft == dis) {
                 resolve()
              } else {
                 if(marginLeft < dis) {
                   marginLeft++
                 } else {
                   marginLeft--
                 }
                 obj.style.marginLeft = marginLeft + 'px' 
                 move()
              }
              
            },10)
        }
        move()
      })
    }
      promiseMove(d1,100)
      .then(function(){return promiseMove(d2,100) })
      .then(function(){return promiseMove(d3,100) })
      .then(function() {return promiseMove(d3,0)})
    .then(function() {return promiseMove(d2,0)})
     .then(function() {return promiseMove(d1,0)})
    }
    
    
    </script>
    </body>
    </html>
    

      

  • 相关阅读:
    jquery遍历table的tr获取td的值
    Java判断文件、文件夹是否存在
    项目搭建系列之三:SpringMVC框架下使用Ehcache对象、数据缓存
    J2EE课程设计:在线书店管理系统
    项目搭建系列之二:SpringMVC框架下配置MyBatis
    使用Git(msysgit)和TortoiseGit上传代码到GitHub
    安卓课程设计:微课表
    项目搭建系列之一:使用Maven搭建SpringMVC项目
    常用markdown语法
    [转]优秀程序员应该做的几件事
  • 原文地址:https://www.cnblogs.com/txxt/p/6104716.html
Copyright © 2011-2022 走看看